Java Regular expression Extract character method instance _ regular expression

Source: Internet
Author: User

Just having a requirement to extract all the specific characters in a string is tedious if you are dealing with a regular string. So I thought of using regular expressions to do it. Project requirements are: need to extract the last number of license plate number, such as: Su A7865 extract 5, su a876x extract 6

Implementation method:

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 as a regular expression, and then the character you want to extract is 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 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 the Matcher:

Mathcer.start ()/Matcher.end ()/Matcher.group ()
When you use Matches (), Lookingat (), find () to perform a matching operation, you can use the above three methods to get more detailed information.
Start () returns the index position of the substring to be matched to in the string.
End () returns the index position of the last character of the substring that is matched to the string.
Group () returns a substring to match

Java code example:

Pattern P=pattern.compile ("\d+"); 
Matcher m=p.matcher ("aaa2223bb"); 
M.find ()///Match 2223 M.start ()// 
return 3 
m.end ();//Return 7, return 2223 index number 
m.group ();//return 2223
mathcer m2= P.matcher ("2223bb"); 
M2.lookingat (); Match 2223 
M2.start ();//return 0, because Lookingat () can only match the preceding string, so when the Lookingat () match is used, the start () method always returns 0 
m2.end ();//returns 4 
M2.group ();//Return to 2223
Matcher m3=p.matcher ("2223"); If Matcher m3=p.matcher ("2223BB"); The following method has an error, because the mismatch returns false 
m3.matches ();///Match the entire string 
m3.start ();//Return 0 
m3.end ();//return 3, cause I believe we all know Because matches () needs to match all strings 
m3.group ();//Return 2223

Said so much, I believe that we all understand the use of the above methods, it is said that the regular expression of the grouping in Java is how to use.
Start (), End (), group () has an overloaded method that is start (int i), end (int i), group (int i) is dedicated to grouping operations, and the Mathcer class also 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 ();//return 2 because there are 2 groups of 
M.start (1);//Return 0 Returns the index number of the substring in the string that matches the first group 
M.start (2);//Return 3 
m.end (1);//Return 3 returns the index position of the last character in the string that matches the first set of substrings. 
M.end (2); Returns 7 
m.group (1);//returns AAA, returns the first set of matched substring 
m.group (2);//returns 2223, returns the substring of the second set of matching strings

Now we're going to use a slightly higher level of the regular matching operation, for example, there is a text, there are a lot of numbers, and these numbers are separate, we now have to take out all the numbers in the text, take advantage of Java's regular operation is so simple.

Java code example:

Pattern P=pattern.compile ("\d+"); 
Matcher M=p.matcher ("My QQ is: 456456 My phone is: 0532214 My mailbox is: aaa123@aaa.com"); 
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 know that each time a match is executed, the values of group () Three methods change, and the information that is changed to the substring that matches to it, as well as their overloaded methods, is changed to the appropriate information.

Note: the start (), End (), group () Three methods can be used only if the matching operation succeeds, or the java.lang.IllegalStateException is thrown, that is, when matches (), Lookingat (), find () when either of these methods returns True, can be used.

The above content gives the Java regular expression extracts the character the method example, hoped has the help to everybody.

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.