Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the analysis of JAVA regular expressions Pattern class and Matcher class

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

JAVA regular expression Pattern class and Matcher class analysis is what, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Java.util.regex is a class library package that matches strings using patterns customized by regular expressions. It includes two classes: Pattern and Matcher Pattern. A Pattern is a compiled representation of a regular expression. Matcher A Matcher object is a state machine that performs a match check on a string according to the Pattern object as a matching pattern. First, a Pattern instance customizes a compiled pattern with a syntax similar to PERL's regular expression, and then a Matcher instance performs string matching under the pattern control of this given Pattern instance.

Let's take a look at these two categories:

I. the concept of capture group

Capture groups can be numbered by calculating their opening brackets from left to right, starting with 1. For example, in the expression (A) (B (C)), there are four such groups:

1 ((A) (B (C) 2 (A) 3 (B (C)) 4 (C)

The group of zeros always represents the entire expression. With (?) The first group is a pure non-capture group that does not capture text and does not count against combinators.

The capture input associated with the group is always the subsequence that closest matches the group. If the group is evaluated again for quantization, its previously captured value (if any) is retained when the second calculation fails, for example, if the string "aba" matches the expression (a (b)?) +, the second group is set to "b". At the beginning of each match, all captured inputs are discarded.

Second, explain the Pattern class and match class in detail

Java regular expressions are implemented through the Pattern class and Matcher class under the java.util.regex package (it is recommended that when reading this article, open the java API document, when introducing which method, check the method description in java API, the effect will be better).

The Pattern class is used to create a regular expression, or a matching pattern, whose constructor is private and cannot be created directly, but a regular expression can be created through the Pattern.complie (String regex) simple factory method.

Java code example:

Pattern p=Pattern.compile ("\\ w+"); p.pattern (); / / return\ w+

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

1.Pattern.split (CharSequence input)

Pattern has a split (CharSequence input) method that separates strings and returns a String []. I guess String.split (String regex) is implemented through Pattern.split (CharSequence input).

Java code example:

Pattern p=Pattern.compile ("\\ d +"); String [] str=p.split ("my QQ is: 456456 my phone number is: 0532214 my email address is: aaa@aaa.com")

Result: str [0] = "my QQ is:" str [1] = "my phone number is:" str [2] = "my email address is: aaa@aaa.com"

2.Pattern.matcher (String regex,CharSequence input) is a static method for fast string matching, which is suitable for matching only once and matching all strings.

Java code example:

Pattern.matches ("\\ d +", "2223"); / / return true Pattern.matches ("\\ d +", "2223aa"); / / return false. You need to match all strings to return true, where aa cannot match to Pattern.matches ("\\ d +", "22bb23"); / / return false, you need to match all strings to return true, here bb cannot match to

3.Pattern.matcher (CharSequence input)

With all that said, it's finally Matcher class's turn, and Pattern.matcher (CharSequence input) returns a Matcher object.

The constructor of the Matcher class is also private and cannot be created at will. The instance of the class can only be obtained through the Pattern.matcher (CharSequence input) method.

The Pattern class can only do some simple matching operations. In order to get a stronger and more convenient regular matching operation, it is necessary to cooperate with Pattern and Matcher. 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 Pattern object by which the Matcher object was created.

4.Matcher.matches () / Matcher.lookingAt () / Matcher.find ()

The Matcher class provides three matching operation methods, all of which return boolean type, true when the match arrives, and false if no match is found.

Matches () matches the entire string and returns true only if the entire string is matched

Java code example:

Pattern p=Pattern.compile ("\\ d +"); Matcher m=p.matcher ("22bb23"); m.matches (); / / returns false because bb cannot be matched by\ d +, resulting in unsuccessful matching of the entire string. Matcher m2=p.matcher ("2223"); m2.matches (); / / returns true because\ d + matches the entire string

Let's now 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 previous string, and only the matching string returns true at the beginning

Java code example:

Pattern p=Pattern.compile ("\\ d +"); Matcher m=p.matcher ("22bb23"); m.lookingAt (); / / returns true, because\ d + matches the previous 22 Matcher m2=p.matcher ("aa2223"); m2.lookingAt (); / / returns false, because\ d + does not match the previous aa

Find () matches the string, and the matching string can be anywhere.

Java code example:

Pattern p=Pattern.compile ("\\ d +"); Matcher m=p.matcher ("22bb23"); m.find (); / return true Matcher m2=p.matcher ("aa2223"); m2.find (); / / return true Matcher m3=p.matcher ("aa2223bb"); m3.find (); / / return true Matcher m4=p.matcher ("aabb"); m4.find (); / / return false

5.Mathcer.start () / Matcher.end () / Matcher.group ()

When you use matches (), lookingAt (), and find () to perform the matching operation, you can use the above three methods to get more detailed information.

Start () returns the index position of the matched substring in the string.

End () returns the index position of the last character of the matched substring in the string.

Group () returns the matching substring

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 the index number m.group () after 2223; / / return 2223 Mathcer m2=m.matcher ("2223bb"); m.lookingAt (); / / match 2223 m.start () / / returns 0, because lookingAt () can only match the previous string, so when using lookingAt () to match, the start () method always returns 0 m.end (); / / returns 4 m.group (); / / returns 2223 Matcher m3=m.matcher ("2223bb"); m.matches (); / / matches the entire string m.start (); / / returns 0, because I believe everyone knows m.end () / / return 6, the reason is clear, because matches () needs to match all the strings m.group (); / / returns 2223bb

Having said so much, I believe we all understand the use of the above methods, and we should talk about how the grouping of regular expressions is used in java.

Start (), end (), group () all have overloaded methods they are start (int I), end (int I), group (int I) are dedicated to grouping operations, and the Mathcer class also has a groupCount () to return how many groups there are.

Java code example:

Pattern p=Pattern.compile ("([a murz] +) (\\ d +)"); Matcher m=p.matcher ("aaa2223bb"); m.find (); / / matching aaa2223 m.groupCount (); / / returns 2 because there are two sets of m.start (1); / / returns 0 returns the index number m.start (2) of the first set of matching substrings in the string; / / returns 3 m.end (1) / / returns 3 returns the index position of the last character of the first set of matched substrings in the string. M.end (2); / / return 7 m.group (1); / / return aaa, return the first set of matched substrings m.group (2); / / return 2223, return the second set of matched substrings

Now we use a slightly more advanced regular matching operation, for example, there is a piece of text, there are a lot of numbers, and these numbers are separate, we now want to take out all the numbers in the text, using the regular operation of java is so simple.

Java code example:

Pattern p=Pattern.compile ("\\ d +"); Matcher m=p.matcher ("my QQ is: 456456 my phone is: 0532214 my email is: aaa123@aaa.com"); while (m.find ()) {System.out.println (m.group ());}

Output:

456456 0532214 123

For example, replace the above while () loop with

While (m.find ()) {System.out.println (m.group ()); System.out.print ("start:" + m.start ()); System.out.println ("end:" + m.end ());}

Then output:

456456 start:6 end:12 0532214 start:19 end:26 123 start:36 end:39

Now you should know that after each matching operation, the values of the three methods start (), end () and group () will change to the information of the matching substring and their overloaded methods.

Note: only when the matching operation is successful, you can use the start (), end (), group () three methods, otherwise java.lang.IllegalStateException will be thrown, that is, when any one of the matches (), lookingAt (), find () methods returns true.

After reading the above, have you mastered the method of analyzing JAVA regular expressions Pattern class and Matcher class? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report