Java regular extraction needs to use the Matcher class, here is a sample case for reference
Need to extract the last number in the license plate number, for example: Su A7865 extract 5, Su a876x extract 6
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Test {
public static void Main (string[] args) {
String s = "a876x";
Write the string you want to match into a regular expression, and then enclose the characters you want to extract using parentheses.
Here, we want to extract the last number, the regular rule is "a number plus greater than or equal to 0 non-numeric plus a Terminator"
Pattern pattern = Pattern.compile ("(\\d) [^\\d]*$");
Matcher Matcher = Pattern.matcher (s);
if (Matcher.find ())
System.out.println (Matcher.group (1));
}
}
A few ways to explain Matcher:
Mathcer.start ()
Matcher.end ()
Matcher.group ()
When you perform a match operation using matches (), Lookingat (), find (), you can use the above three methods to get more detailed information.
Start () returns the index position of the substring that matches to the string.
End () returns the index position of the last character in the string that matches the substring.
Group () returns the substring that is matched to
The sample code is as follows, please refer to the note for specific functions
Pattern p=pattern.compile ("\d+");
Matcher m=p.matcher ("AAA2223BB");
M.find ();//Match 2223
M.start ();//Return 3
M.end ();//returns 7, returns the index number after 2223
M.group ();//Return 2223
Mathcer m2=p.matcher ("2223BB");
M2.lookingat (); Match 2223
M2.start (); Returns 0 because Lookingat () can only match the preceding string, so when Lookingat () is used, the start () method always returns 0
M2.end (); Returns 4
M2.group (); Returns 2223
Matcher m3=p.matcher ("2223"); If Matcher m3=p.matcher ("2223BB"); Then the following method is faulted because the mismatch returns false
M3.matches (); Match entire string
M3.start (); Returns 0
M3.end (); Return 3, reason to believe that everyone is also clear because matches () needs to match all the strings
M3.group (); Returns 2223
In addition, start (), End (), group () in the Mathcer class have an overloaded method that they are start (int i), end (int i), group (int i) are dedicated to the grouping operation, and the Mathcer class has a groupcount () Used to return how many groups there are.
Examples are as follows:
Pattern P=pattern.compile ("([a-z]+) (\d+)");
Matcher m=p.matcher ("AAA2223BB");
M.find (); Match aaa2223
M.groupcount (); Returns 2 because there are 2 groups
M.start (1); Returns 0 returns the index number in the string of the first set of matched substrings
M.start (2); Returns 3
M.end (1); Returns 3 returns the index position in the string of the last character of the first set of matched substrings.
M.end (2); Returns 7
M.group (1); Returns AAA, returning the first set of substrings to match
M.group (2); Returns 2223, returning the second set of substrings to match
Note: Only if the match operation succeeds can you use Start (), End (), group () three methods, otherwise it will throw java.lang.IllegalStateException, that is, when matches (), Lookingat (), Find () can be used if either of the methods returns True.
Java regular match