Regular expression | Java | Class content organization

        Never mind, the blogger is a lazy pig. I didn’t go to class last week, and I just started watching the ppt on November 3rd. I didn’t start writing until after 11 o’clock in the evening on November 4th. There are so many people and lazy , to waste power (sorrow)


This is a compilation of notes about the use of regular expressions in Java. The original content comes from Mr. Liu Bin's class fart kick.

1. Using regular expressions in Java

Usually, there are many methods in the String class that support regular expressions.

1、matches()方法:可以验证字符串是否符合给定的正则表达式,返回值是一个boolean值。例如

String regular = "[0-9]*"; //表示字符串仅由数字组成
String str = "114514";
if ( str.matches(regular) ) {
    System.out.println("YES");
}

运行上面的代码,得到的结果是YES.


2、replaceAll()方法:可以将符合正则表达式的子字符串置换为指定的字符串。例如

String regular = "[0-9]"; //表示一个数字
String str = "There are 50 students in 1 classroom.";
str = str.replaceAll(regular, "REPLACED");
System.out.println(str);

运行上面的代码,得到的结果是
There are REPLACEDREPLACED students in REPLACED classroom.

类似有replace,replaceFirst等方法。

3、split()方法:依指定的正则表达式,将符合的子字符串排除,剩下的子字符串分离出来并以字符串数组返回。例如

String regular = "[1234567890]";
String str = "There50in2classrooms.";
for (String sub: str.split(regular)) {
    System.out.println(sub);
}

运行上面的代码,得到的结果是
There

in
classrooms.

2. Regular expressions

What we need to know are ordinary characters, metacharacters and greedy quantifiers.

① Ordinary characters are commonly used characters, such as a letter, a number, a Chinese character, etc.;

② However, in practical applications, it is often necessary to match any number or letter, any number of consecutive characters, etc. It is obviously inconvenient to use ordinary characters at this time, and metacharacters are used at this time. For example, \d can replace any number, and \ is an escape character. If there is no \, but only one d, it means the lowercase letter d of ordinary characters.

Commonly used metacharacters and greedy quantifiers are

\ . * + - { } [ ] ^ $ | ? ( ) : ! =

1、\
转义字符。
例如\d表示数字,\D表示非数字字符。

2、.
匹配除换行符(\n,\r)以外的其他任何单个字符。
例如d.t可以匹配dat,dot,dnt,d~t等。

3、*
匹配前面的子表达式零次或多次。
例如do*可以匹配d,do,doo,dooo,...

4、+
匹配前面的子表达式一次或多次。
例如do*可以匹配do,doo,dooo,...。不能匹配d。

5、-
表示字符范围。例子参考第7条。
特别提示:[50-99]等价于[0-9],这里的50和99实际上都会被认为是ASCLL码。

6、{n:m}
n和m是非负整数,且n≤m。匹配前面的子表达式确定的n次到m次。
例如T{3}匹配TTT,不能匹配TT,不能匹配TTTT。
例如T{3:5}匹配TTT或TTTT或TTTTT。
例如T{3:}匹配TTT,TTTT,TTTTT,TTTTTT,...

7、[xyz]
字符集合。匹配所包含的任何一个字符。
例如[aeiou]匹配"play"中的'a'。
例如[0-9]匹配"pl4y"中的'4'。

8、^
一种方式理解为非。
例如[^0-9],匹配非数字。

另一种方式表示字符串的开始位置。
例如^[0-9]+[a-z]*表示以至少一个数字开头,后接任意个小写字母的字符串。

9、$
表示字符串结束位置。
例如^[0-9]+[a-z]$表示以至少一个数字开头,并以一个小写字母结尾的字符串。

10、|
逻辑或
例如[a-z]|[A-z]匹配字母。

11、?
匹配前面的子表达式零次或一次。
例如do(es)?可以匹配do或者does

12、()
通常意义上的括号。把若干个字符当成一个整体。例子参考第11条。

13、: ! = 看图

 (Picture source rookie.)

For some other special cases, see Fig.


Regular expressions are very beautiful. From the first time I saw it, I thought it was light and convenient, but it is not easy to use it skillfully.

Guess you like

Origin blog.csdn.net/m0_70241024/article/details/127698330