Detailed regular expressions

In this post, I will explain knowledge and a platform-independent - regular expressions.
For the students this term may be very strange, but I believe the students will be very sensitive to "climb" the word.
Yes, this post to explain the knowledge points, can be applied to "climb" out of scene information we need in.
Well, ado, we now begin the main content of this blog post to explain it!



First, I explain what is the regular expression :

definition:

Definition: The so-called regular expression means:
a single string matching is used to describe a series of line or a character string syntax rules. It is actually a rule .

Methods In Java, the processing of regular expressions in java.util.regex Pattern class in.

So, in the understanding of the definition of regular expressions after I introduce regular expression composition rules :


Composition rules:

A: characters :
the X-character x. Example: 'a' represents the characters A
\ backslash character.
\ n new line (line feed) ( '\ u000A')
\ R & lt carriage return ( '\ u000D')
------------------------- -------------------------------------------------- -
B: character class :
[ABC] a, b or C (simple)
[^ ABC] any character, in addition to a, b or C (negative)
[A-zA-the z] A to z or A to z, two including letters (range)
character [0-9] comprises 0 to 9
------------------------------ ----------------------------------------------
C: predefined character class :
any character, then, how you express it?.
\ d digit: [0-9]
\ w word character: [a-zA-Z_0-9]
things to form words in a regular expression which must have these things make up
\ s matches a space character
--------------------------------------------- --------------------------------
D:Boundary matcher :
the beginning of the line ^
end of the line $
\ b word boundary
is not a word character place.
-------------------------------------------------- --------------------------
E: Greedy quantifiers :
? the X-the X-, or once there is no such "" empty string is not
X * X , zero or more times greater than 1 are considered equal times
X + X, one or more
X {n} X, exactly n times
X {n,} X, at least n times
X {n, m} X, at least n times, but not more than m times

Here, I again through the code and comments to explain the above-mentioned composition rules:

    regx="[a,b,c,d,e,1,2,3]"; //是列表中的某一个字符就行
    regx="[a-z]"; //是小写字母就行
    regx="[0-9]"; //是数字字符就行
    regx="\\d";  // 这个等同于 [0-9]
    regx="[A-Z]"; //是大写字母就行
    regx="[a-zA-Z0-9]"; //是26个大小写字母 或者 数字之一就行
    regx="\\w"; //等同于 [a-zA-Z_0-9]
    
    regx="."; //. 匹配单个任意字符
    regx="\\."; // \\ 转义符,这里的“\\.”表示只匹配“.”字符
    regx = ".."; // 表示匹配两个任意字符
    
    regx = "[^A-Z]"; //取反,不是大写字母就行
    regx="|"; // |表示 或者关系
    regx="\\|"; // 这里的 “\\.” 表示只匹配“|”字符
    regx="&"; // &表示 与关系
    
    regx="\\s"; //匹配空格
    
    //量词  ? 一次或一次也没有  空串就是没有出现
    regx="a?";
    
    //* :零次或多次 一次也算多次
    regx="[a-z]*";
    
    //+ :一次或多次
    regx="[0-9]+";
    regx="\\w+";
    
    //恰好几次
    regx="[0-9]{5}";    //表示只匹配5个数字字符的字符串
    
    //至少几次
    regx="[a-z]{5,}";   //表示只匹配至少5个但只能全是小写字母字符的字符串
    
    //一个范围 大于等于5 小于等于10
    regx = "[a-z]{5,10}"; //表示只匹配 5~10个小写字母 组成的字符串

Well, now it would have been an example to show to the students about the "regular expression" basic use:
Now we ask: the source string equal sign all removed

package com.youzg.about_regular_expression.test

public class DeleteEqualSign {
    public static void main(String[] args) {
        String str="aaaa=bbbb=cccc=dddd";  //aaaabbbbcccdddd;
        String replace = str.replace("=", "");
        System.out.println(replace);
    }
}

So, we look at the results:
Here Insert Picture Description
So, through this example, I believe that students have the knowledge to have a preliminary understanding.
Next, I have come to bring the students to consolidate it through a few examples of the use of these methods:
Requirements: to determine whether the user entered a valid email

package com.youzg.about_regular_expression.test

import java.util.Scanner;

public class CheckE-mail {

    public static void main(String[] args) {
        String emailRegx="[a-zA-Z]\\w{5,17}@(163|sina|google)\\.(com|cn|net|org)";

        Scanner sc = new Scanner(System.in);
        String inputString = sc.nextLine();
        boolean matches1 = inputString.matches(emailRegx);
        System.out.println(matches1);
    }
    
}

Now, let's look at the results:
Here Insert Picture Description

Requirements: Digital given string to sort

package com.youzg.about_regular_expression.test

import java.util.Arrays;

public class SortString {
    public static void main(String[] args) {
/*
*
*  * 分析:
 * a: 定义目标字符串"91 27 46 38 50"
 * b: 对这个字符串进行切割,得到的就是一个字符串数组
 * c: 把b中的字符串数组转换成int类型的数组
 * (1): 定义一个int类型的数组,数组的长度就是字符串数组长度
 * (2): 遍历字符串数组,获取每一个元素.将其转换成int类型的数据
 * (3): 把int类型的数据添加到int类型的数组的指定位置
 * d: 排序
 * e: 创建一个StringBuilder对象,用来记录拼接的结果
 * f: 遍历int类型的数组, 将其每一个元素添加到StringBuilder对象中
 * g: 就是把StringBuilder转换成String
 * h: 输出
*
*
* */
        String str = "91    27    46   38    50000      50  5  9  9000";
        String[] strings = str.split("\\s+");

        int[] ints = new int[strings.length];

        for (int i = 0; i < strings.length; i++) {
            ints[i]=Integer.parseInt(strings[i]);
        }
        //排序
        Arrays.sort(ints);
        //遍历 int 数组 凭借成字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ints.length; i++) {
            sb.append(ints[i]).append(" ");
        }
        String string = sb.toString().trim();
        System.out.println(string);
    }
}

Let's watch the next result:
Here Insert Picture Description
Well, now, I have to introduce a regular expression is very powerful - Replace function :

Replace regular expression functions:

This knowledge, I have to show by an example:
Requirements: Digital given string are removed

package com.youzg.about_regular_expression.test

public class RemoveDigit {

    public static void main(String[] args) {
        String str2 = "aaaa=1111125522bbbb55444444cccc6665444444dddd";
        //根据正则表达式去替换
        String s = str2.replaceAll("[0-9]+", "");
        System.out.println(s);

    }
    
}

Here Insert Picture Description

So, by the above three examples, I believe the students for regular use basic methods of expression are already familiar with, and now, I am the last one to explain the knowledge about regular expressions - Regular expressions acquisition function

Regular expressions acquisition function:

In this function before the introduction of regular expressions, I am the first to introduce the Pattern and Matcher two classes:
For these two categories of knowledge, we do not need to know too much, as long as the order clearly used to;
a typical calling sequence Yes:

    Pattern p = Pattern.compile(正则表达式);
    Matcher m = p.matcher(待检测表达式);
    boolean b = m.matches();

So, now, it is an example to show how the regular expression acquisition function;
Requirements: sub-three-letter given in string and outputs the extracted pick:

package com.youzg.about_regular_expression.test

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetTheActivePart {
    public static void main(String[] args) {
        //需求:获取下面这个字符串中由三个字符组成的单词
        String str="da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
       //1.三个字符组成的单词 的正则  [a-z]{3}
        //2.模式器和匹配器配合起来,可以根据正则获取出符合正则的字符串

        //获取模式器
        Pattern p = Pattern.compile("\\b[a-z]{3}\\b");

        //获取匹配器
        Matcher matcher = p.matcher(str);
        
        while (matcher.find()){
            String group = matcher.group();
            System.out.println(group);
        }
    }
}

Results are as follows:
Here Insert Picture Description
It is also because regular expressions acquisition function, as well as knowledge of front-end, we are able to "climb" out of the information we want to know.

Guess you like

Origin www.cnblogs.com/codderYouzg/p/12418306.html
Recommended