Regular Expressions
(a) What is a regular expression?
A string that conforms to a certain rule.
(ii) Common rules
A: Character
x character X. such as: ' 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. If it is the. Character itself, this means: \.
\d number: [0-9]
\d non-numeric: [^0-9]
\s whitespace characters:[\t\n\x0b\f\r]
\s non-whitespace characters:[^\s]
\w Word character: [a-za-z_0-9], the regular expression inside the word composition must consist of these things
D: Boundary matching device
^ The beginning of the line
End of the $ line
\b Word boundary, is not a word character place. such as: Java:hello world?
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
(c) Common functions:
A: Judging function
Public boolean matches of the String class (string regex)
Code Case:
String regex = "[1-9][0-9]{4,14}";
public boolean matches (string regex) tells whether this string matches a given regular expression
Boolean flag = Qq.matches (regex);
return flag;
return Qq.matches ("[1-9][0-9]{4,14}"), or return Qq.matches ("[1-9]\\d{4,14}");
B: Split function
Public string[of the String class] split (string regex)
Code Case:
String s = "aa.bb.cc";
string[] Strarray = s.split ("\ \");
for (int x = 0; x < strarray.length; × x + +) {
System.out.println (Strarray[x]);
}
C: Replace function
public string ReplaceAll of the String class (String regex,string replacement)
Code Case:
String s = "ab345world67890xyz!";
Replace all numbers with a *,
String regex = "\\d+";
String regex = "\\d";
String SS = "*";
String result = S.replaceall (regex, SS);
SYSTEM.OUT.PRINTLN (result);
Remove the numbers directly
String regex = "\\d+";
String ss = "";
String result = S.replaceall (regex, SS);
System.out.println (result);
D: Get Features
Pattern and Matcher
Compiling regular expressions into schema objects
Pattern p = pattern.compile ("A*b");
Getting a match object from a pattern object
Matcher m = P.matcher ("Aaaaab");
The function of the match object:
Public boolean Find(): Find content that matches the pattern does not exist, and returns true only if the subsequence of the input sequence matches the pattern of this match
public String Group(): Returns the input subsequence that was matched by the previous match operation
Note: Find () First, then group ()
Code case: Getting a three-character word in a given string
String s = "ni Zai zuo shen me?";
String regex = "\\b\\w{3}\\b";
Compiling the rules into a schema object
Pattern p = pattern.compile (regex);
//Get a Match object with a pattern object
Matcher m = P.matcher (s);
//Find substrings that have not met the criteria through the Find () method
public Boolean find ()
Boolean flag = M.find ();
SYSTEM.OUT.PRINTLN (flag);
Gets the substring found by find () using the group () method
Public String Group ()
String ss = M.group ();
SYSTEM.OUT.PRINTLN (ss);
while (M.find ()) {
System.out.println (M.group ());
}
Java Regular Expressions