(Common API) regular expression syntax rules

Regular expression matching rules

Referring to the help documentation, there are rules define regular expressions in the Pattern class, regular expressions a clear distinction between uppercase and lowercase letters. Let's learn the rules of grammar.

Regular expression syntax rules:

Character: the X-

Meaning: represents the character x

For example: matching rule is "a" , then the content needs to match the string is "a"

 

Character: \\

Meaning: represents the backslash character '\'

For example: matching rule is "\\" , you need to match the contents of the string is "\"

 

Character: \ t

Meaning: Tabs

For example: matching rule "\ T ", then the corresponding effect is to create a space of a tab

 

Character: \ the n-

Meaning: newline

For example: matching rule "\ n-" , then the corresponding effect is to change the line, the next line in the original cursor position

 

Characters: \ r

Meaning: a carriage return

For example: matching rule is "\ r" , then the corresponding effect is the effect after the carriage return, cursor to next line

 

Character class: [abc]

Meaning: represents the character A , b or c

Matching rules: for example "[abc]" a, then the content is the need to match the characters a, B or character, or a character c

 

Character class: [^ abc]

Meaning: that in addition to representatives of A , b or c other than any character

For example: matching rule "[^ ABC]" , then the content is not required to match the characters a, or not characters b, c or not the character of any character

 

Character class: [A-zA-the Z]

Meaning: represents a to z or A to the Z , two letters included

For example: matching rule "[A-zA-the Z]" , it is a need to match uppercase and lowercase letters

 

Character class: [0-9]

Meaning: represents 0 Dao 9 numbers, including the numbers of two

For example: matching rules "[0-9]" , then you need to match a digital

 

Character class: [A-zA-Z ~ 0-9]

Meaning: letters or numbers or underscore representative ( ie word characters )

For example: matching rule "[A-zA-Z ~ 0-9]" , then the match is needed is a letter or a digit line or a decline in

 

Predefined character classes: .

Meaning: stands for any character

For example: matching rules . "" , You need to match an arbitrary character. If you wanted to use. Then, using the matching rules "\\." To achieve

 

Predefined character classes: \ d

Meaning: a is 0 to . 9 figures, including the two figures, corresponding to [0-9]

For example: matching rules "\ d" , then you need to match a digital

 

Predefined character classes: \ w

Meaning: letters or numbers or underscores representative ( i.e., word characters ) , equivalent to [a-zA-Z_0-9]

For example: matching rule "\ W" ,, it is required to match a number or a letter or a decline in line

 

Boundary matcher: ^

Meaning: represents the beginning of the line

For example: matching rule ^ [abc] [0-9] $ , then the content needs to be matched from the beginning [abc] this position is equivalent to a left double quotation mark

 

Boundary matcher: $

Meaning: represents the end of the line

For example: matching rule ^ [abc] [0-9] $ , then the content needs to match [0-9] this end, the equivalent of closing double quotation mark

 

Boundary matcher: \ b

Meaning: represents the word boundary

For example: matching rule "\ b [ABC] \ b" , it represents a non-desired word characters left and right letter a or b, or c ( [a-zA-Z ~ 0-9] )

 

Quantifier: the X-?

Meaning: represents the X occur once or even once

For example: matching rule is "a?" , Then you need to match the contents of a character is a, or a not a

 

Quantifier: the X-*

Meaning: stands for X appear zero or more times

For example: matching rule is "a *" , you need to match the contents of more than one character is a, or a not a

 

Quantifiers: the X-+

Meaning: it stands for X one or more occurrences

For example: matching rule "a +" , then the need to match the contents of the plurality of characters a, a, or a

 

Quantifier: X-n-{}

Meaning: stands for X appear exactly n times

For example: matching rule "{a}. 5" , then the need to match the contents of five characters a

 

Quantifier: X-n-{,}

Meaning: represents the X appears at least n times

For example: matching rule ". 5 {a,}" , then the need to match the content is a minimum of five characters

 

Quantifier: X-n-{,} m

Meaning: represents the X appears at least n times, but not more than m times

For example: matching rule "a {5,8}" , then there is a need to match the contents of between five to eight characters in a character a

 

Regular expression syntax rules

* A: 正则表达式语法规则
	* a: 字符
		* x  代表的是字符x
		* \\ 代表的是反斜线字符'\'
		* \t 代表的是制表符
		* \n 代表的是换行符
		* \r 代表的是回车符
	* b: 字符类
		* [abc]    a、b 或 c(简单类)
		* [^abc]   任何字符,除了 a、b 或 c(否定)
		* [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
		* [0-9]    0到9的字符都包括
		* [a-zA-Z_0-9] 代表的字母或者数字或者下划线(即单词字符)
	* c: 预定义字符类
		* . 任何字符。
		* \d 数字:[0-9]
		* \w 单词字符:[a-zA-Z_0-9]如"com.itheima.tests"/finish
	* d: 边界匹配器
		* ^  代表的是行的开头
		* $  代表的是行的结尾
		* \b 代表的是单词边界
	* e: 数量词
		* X?     X,一次或一次也没有
		* X*     X,零次或多次
		* X+     X,一次或多次
		* X{n}   X,恰好 n 次 
		* X{n,}  X,至少 n 次 
		* X{n,m} X,至少 n 次,但是不超过 m 次

 

Released 2417 original articles · won praise 62 · Views 200,000 +

Guess you like

Origin blog.csdn.net/Leon_Jinhai_Sun/article/details/105173840