Explain the pattern class and matcher class in Java regular expressions in detail

preface

This article will introduce the pattern class and matcher class in Java regular expressions. First, we need to make it clear that the regular expression specified as a string must first be compiled into an instance of the pattern class. Therefore, programmers must know how to better understand these two classes.

Let's take a look at these two classes:

1、 Capture group concept

Capture groups can be numbered by calculating their open parentheses from left to right, starting with 1. For example, in the expression ((a) (b (c))), there are four such groups:

A group of zeros always represents the entire expression. With (?) The first group is a pure non capture group, which does not capture text and does not count against the combination meter.

The capture input associated with a group is always the subsequence that most recently matches the group. If the group is evaluated again due to quantization, its previously captured value (if any) will be retained when the second calculation fails. For example, combine the string "ABA" with the expression (a (b)?)+ Match, the second group is set to "B". At the beginning of each match, all captured inputs are discarded.

2、 Explain the pattern class and matcher class in detail

Java regular expression through Java util. Implementation of pattern class and matcher class under regex package (it is recommended to open the Java API document when reading this article. When introducing which method, check the method description in the Java API, and the effect will be better)

The pattern class is used to create a regular expression, or a matching pattern. Its construction method is private and cannot be created directly, but it can be created through pattern The simple factory method of complex (string regex) creates a regular expression,

Java code example:

Pattern () returns the string form of a regular expression. In fact, it returns pattern Regex parameter of complex (string regex)

1.Pattern. split(CharSequence input)

Pattern has a split (charsequence input) method to separate strings and return a string [], I guess string Split (string regex) is through pattern Split (charsequence input)

Java code example:

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

2.Pattern. Matcher (string regex, charsequence input) is a static method used to quickly match strings. This method is suitable for matching all strings only once

Java code example:

3.Pattern. matcher(CharSequence input)

After talking so much, it's finally the matcher class, pattern Matcher (charsequence input) returns a matcher object The construction method of matcher class is also private and cannot be created at will. It can only be created through pattern The matcher (charsequence input) method obtains an instance of this class Pattern class can only do some simple matching operations. To get stronger and more convenient regular matching operations, we need to cooperate with pattern and matcher Matcher class provides grouping support for regular expressions and multiple matching support for regular expressions

Java code example:

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

The matcher class provides three matching operation methods. All three methods return Boolean type. When matching, they return true. If no matching is found, they return false

Matches () matches the entire string, and returns true only when the entire string matches

Java code example:

Now 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 previous string, and returns true only if the matched string is at the front

Java code example:

Find () matches the string, and the matched string can be in any position

Java code example:

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

When you use matches(), lookingat(), 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:

Having said so much, I believe everyone understands the use of the above methods. It's time to talk about how regular expression grouping is used in Java Start(), end(), group() all have an overloaded method. They are start (int i), end (int i), and group (int i) dedicated to grouping operations. Mathcer class also has a groupcount() to return the number of groups

Java code example:

Now let's use a slightly more advanced regular matching operation. For example, there is a text with many numbers, and these numbers are separated. Now we need to take out all the numbers in the text. It's so simple to use the regular operation of Java

Java code example:

Output:

For example, replace the while() loop above with

Output:

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

Note: only when the matching operation is successful can the three methods start(), group() be used, otherwise Java. Net will be thrown Lang. IllegalStateException, that is, it can be used only when any of the methods of matches(), lookingat(), find() returns true

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>