Java Regular Expressions Extract characters

Source: Internet
Author: User

Requirements: Extract the last number of license plate number, such as: Ning A7865 extract 5, ning a876x extract 6
Implementation method:

import Java.util.regex.matcher;import java.util.regex.pattern;< Span class= "Hljs-keyword" >public  class  Test { Public  static  void  main  (string[] args)        {String s =  "a876x" ;        //writes the string to match as a regular expression, and then the characters to be extracted are enclosed in 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 ();                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
Java code example:
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

Say so much, I believe we all understand the use of the above several methods, it is said that the regular expression of the grouping in Java is how to use.
Start (), End (), group () have an overloaded method they are start (int i), end (int i), group (int i) are dedicated to the grouping operation, and the Mathcer class has a groupcount () to return the number of groups.
Java code example:
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

Now let's use a regular matching operation with a slightly higher point, such as a text with a lot of numbers in it, and the numbers are separate, we're now going to take all the numbers out of the text, and it's easy to take advantage of Java's regular operations.
Java code example:
Pattern p=pattern.compile ("\d+");
Matcher M=p.matcher ("My QQ is: 456456 My phone is: 0532214 my mailbox is: [email protected]");
while (M.find ()) {
System.out.println (M.group ());
}

Output:
456456
0532214
123

If you replace the above while () loop with the
while (M.find ()) {
System.out.println (M.group ());
System.out.print ("Start:" +m.start ());
System.out.println ("End:" +m.end ());
}
The output:
456456
Start:6 End:12
0532214
Start:19 end:26
123
Start:36 end:39

Now you should be aware that each time the match operation is performed, start (), End (), group () Three method values change, the information that changes to the substring to match to, and their overloaded methods, will also change to the corresponding information.
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 Expressions Extract characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.