Regular Expressions (understanding)
(1) A string that conforms to a certain rule
(2) Common rules
A: Character
x character X. Example: ' A ' denotes character a
\ \ backslash character.
\ n New Line (newline) character (' \u000a ')
\ r return character (' \u000d ')
B: Character class
[ABC] A, B or C (simple Class)
[^ABC] Any character except A, B, or C (negation)
[A-za-z] A to Z or A to Z, the letters at both ends are included (range)
[0-9] 0 to 9 characters are included
C: Predefined character classes
. Any character. And mine is. The character itself, how to express it? \.
\d number: [0-9]
\w Word character: [a-za-z_0-9]
Things that make up words in regular expressions must have these things.
D: Boundary Matching device
^ The beginning of the line
End of the $ line
\b Word boundaries
is the place where the word character is not.
Example: Hello World?haha;xixi
E:greedy number of words
X? X, not once or once
X* X, 0 or more times
x+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n times, but no more than m times
(3) Common function: (who is it used separately?)
A: Judging function
Public boolean matches of the String class (string regex)
B: Split function
Public string[of the String class] split (string regex)
C: Replace function
public string ReplaceAll of the String class (String regex,string replacement)
D: Get Features
Pattern and Matcher
Pattern p = pattern.compile ("A*b");
Matcher m = P.matcher ("Aaaaab");
Find (): Lookup does not exist
Group (): Get the data you just looked up
Example one: 5 to 15 digits are required. Cannot start at 0
Enter a string from the keyboard scanner Sc=new scanner (system.in); String str=sc.nextline ();//string regex= "[1,9][0,9]{4,15}";//boolean flag=str.matches (Regex); Boolean flag= Str.matches ("^[1,9][0,9]{4,15}$");
Common Regular Expressions