A practical tutorial on Java Regular expressions

Source: Internet
Author: User

Java.util.regex is a class library package that matches strings by using regular expression-ordered patterns.
The Java.util.regex package consists of the following three classes: Pattern, Matcher, and Patternsyntaxexception.

    1. A pattern is a pattern of expressions compiled by a regular expression.
    2. A Matcher object is a state machine that matches a string to a matching pattern based on pattern objects.
    3. Patternsyntaxexception is a non-mandatory exception class that represents a syntax error in a regular expression pattern.

First, a pattern instance is used to customize a compiled pattern of a similar regular expression using the same syntax as Perl.
Then a Matcher instance matches the string in the pattern control of the given pattern instance.

Capturing groups

A capturing group is a method of processing multiple characters as a single unit, which is created by grouping characters within parentheses.

For example, the regular expression (dog) creates a single group that contains "D", "O", and "G".

Capturing groups are numbered by calculating their opening brackets from left to right. For example, in an expression ((A) (B (C))), there are four such groups:

    • ((A) (B (C)))
    • A
    • (B (C))
    • C

You can see how many groupings of an expression by calling the GroupCount method of the Matcher object. The GroupCount method returns an int value that indicates that the Matcher object currently has more than one capturing group.

There is also a special group (group 0), which always represents the entire expression. The group is not included in the return value of GroupCount.

Detailed pattern class and Matcher class

Java regular expressions are implemented by the pattern class and the Matcher class under the Java.util.regex package (it is recommended to open the Java API documentation when reading this article, and to see the method descriptions in the Java API when it comes to this document).
The pattern class is used to create a regular expression, or it can be said to create a matching pattern, which is constructed privately and cannot be created directly, but can be created by using the Pattern.complie (String regex) Simple factory method to create a regular expression.
Java code example:

Pattern P=pattern.compile ("\\w+");p. Pattern (); // back to \w+


Pattern () returns the string form of a regular expression, which is actually the regex parameter that returns Pattern.complile (string regex)

1.pattern.split (charsequence input)

Pattern has a split (Charsequence input) method that separates the string and returns a string[], and I guess String.Split (String regex) is through Pattern.split ( Charsequence input) to achieve this.
Java code example:

Pattern p=pattern.compile ("\\d+"); string[] Str=p.split ("My QQ is: 456456 My phone is: 0532214 my mailbox is: [email protected]"); Result: str[0]= "My qq is:" str[1]= "My phone number is:" Str[2]= "My mailbox is: [Email protected]"


2.pattern.matcher (string regex,charsequence input) is a static method that is used to quickly match a string, which is suitable for matching only once and matching all strings.

Java code example:

Pattern.matches ("\\d+", "2223"); // returns True Pattern.matches ("\\d+", "2223AA"); // returns false, which needs to be matched to all strings to return true, where AA cannot match to Pattern.matches ("\\d+", "22bb23"); // returns false, which needs to be matched to all strings to return true, where BB cannot match to


3.pattern.matcher (charsequence input)

Having said so much, it was finally the Matcher class, Pattern.matcher (Charsequence input) returned a Matcher object.
The construction method of the Matcher class is also private and cannot be created at will, only an instance of the class can be obtained through the Pattern.matcher (Charsequence input) method.
The pattern class can only do a few simple matching operations, in order to get stronger and more convenient regular matching operations, it is necessary to work with the pattern and Matcher. The Matcher class provides grouping support for regular expressions and multiple matching support for regular expressions.
Java code example:

Pattern p=pattern.compile ("\\d+"); Matcher m=p.matcher ("22bb23"); M.pattern (); // returns p that is the Matcher object that is created by which pattern object is returned.


4.matcher.matches ()/Matcher.lookingat ()/Matcher.find ()

The Matcher class provides three matching operation methods, three methods return a Boolean type, and return true when matched to, false if not matched to
Matches () matches the entire string and returns true only if the entire string matches.
Java code example:

Pattern p=pattern.compile ("\\d+"); Matcher m=p.matcher ("22bb23"); m.matches (); // returns false because the BB cannot be \d+ matched, causing the entire string match to be unsuccessful. Matcher m2=p.matcher ("2223"); m2.matches (); // returns True because \d+ matches the entire string


Let's look back at Pattern.matcher (String regex,charsequence input), which is equivalent to the following code
Pattern.compile (regex). Matcher (Input). Matches ()

Lookingat () matches the preceding string, only the string that matches to the front returns true
Java code example:

Pattern p=pattern.compile ("\\d+"); Matcher m=p.matcher ("22bb23"); M.lookingat (); // returns True because \d+ matches the previous Matcher m2=p.matcher ("aa2223"); M2.lookingat (); // return False, because \d+ cannot match the previous AA


Find () matches a string that matches to a string that can be anywhere.
Java code example:

Pattern p=pattern.compile ("\\d+"); Matcher m=p.matcher ("22bb23"); M.find (); // returns True Matcher m2=p.matcher ("aa2223"); M2.find (); // returns True Matcher m3=p.matcher ("AA2223BB"); M3.find (); // returns True Matcher m4=p.matcher ("Aabb"); M4.find (); // returns false


5.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 2223M.start ();//returns 3M.end ();//returns 7, returns the index number after 2223M.group ();//back to 2223mathcer m2=m.matcher ("2223BB"); M.lookingat (); //Match 2223M.start ();//returns 0 because Lookingat () can only match the preceding string, so when Lookingat () is used, the start () method always returns 0M.end ();//returns 4M.group ();//back to 2223Matcher M3=m.matcher ("2223BB"); m.matches (); //Match entire stringM.start ();//return 0, cause I'm sure everyone knows.M.end ();//return 6, reason to believe that everyone is also clear because matches () needs to match all the stringsM.group ();//return 2223BB


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 aaa2223M.groupcount ();//returns 2 because there are 2 groupsM.start (1);//returns 0 returns the index number in the string of the first set of matched substringsM.start (2);//returns 3M.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 7M.group (1);//returns AAA, returning the first set of substrings to matchM.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:4564560532214123


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 ());} Then output:456456start: 6 end:120532214start: end:26123start: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.

A practical tutorial on Java Regular expressions

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.