Regular expression's regular character:
A: Character
x character X, any character represents itself.
\ \ backslash Character
\ r Enter
\ nthe line break
B: Character class
[ABC] A, B, or C, any character at a time.
[^ABC] Any character except A, B, or C
[A-za-z] A to Z or A to Z, the letters at both ends are included
[0-9] Arbitrary numeric characters at once
C: Predefined character classes
. Any of the characters
\d number: [0-9]
\w Word character: [a-za-z_0-9]
Word characters: English, numerals, and _
D: Boundary Matching device
^ The beginning of the line
End of the $ line
\b Word boundaries (that is, it cannot be a word character)
ABC Hello World?haha
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
F: Group
Capturing groups can be numbered by calculating their opening brackets from left to right. Group 0 always represents an entire expression.
((A) (B (C)))
First group: (A) (B (C))
Group II: A
Group III: B (C)
Group Fourth: C
1 ImportJava.util.regex.Matcher;2 ImportJava.util.regex.Pattern;3 4 //application of Pattern class5 Public classPatterndemo {6 Public Static voidMain (string[] args) {7Pattern p = pattern.compile ("A*b");//Regular expressions are represented in parentheses8Matcher m = P.matcher ("C*b");//the string to match in parentheses9 BooleanFlag =m.matches ();Ten System.out.println (flag); OneSystem.out.println ("-------------------------------------------"); A -String regex = "(.) \\1+ ";//notation of overlapping words, regular expressions -Pattern P1 =pattern.compile (regex); theString str = "JSKKJJVJKSVKJSJKGAHGGGGHGAAAYF"; -Matcher M1 =P1.matcher (str); - BooleanFlag2 = M1.matches ();//finding out overlapping words - System.out.println (FLAG2); + while((M1.find ())) {//whether to find, to judge, to automatically move the pointer to the next index -String strtemp = M1.group ();//get to find out the results + System.out.println (strtemp); A } atSystem.out.println ("-------------------------------------------"); -string[] Strarr = Str.split (regex);//segmentation based on overlapping words - for(String strtemp:strarr) { - System.out.println (strtemp); - } -System.out.println ("-------------------------------------------"); in } -}
Java Basics-Day 26th