Regular Expressions: Pattern and mtcher

Source: Internet
Author: User

The Java. util. RegEx package is a class library package used to match character sequences and regular expressions. This package contains three classes: pattern, matcher, and patternsyntaxexception.

Pattern: Expression Pattern After regular expression Compilation

Matcher: A matcher object is a state machine that performs a matching check on strings Based on the pattern object.



Procedure:

First, use the static method complie of pattern to create the pattern object.

Pattern n = pattern. complie (regax );

Then, call the Pattern Method matcher.

Matcher M = P. matcher (candidate );

Get the matcher object. The matcher object stores a lot of matching information and searches for the matching part through the find () method. If yes, true, M is returned. group () can obtain values of each group. Otherwise, false is returned.




Methods of the pattern class:

1. Static pattern compile (string RegEx );

Compile the given regular expression and assign it to the parrern class;

Static pattern compile (string RegEx, int flags );

2. Int flags ();

Returns the flags parameter matching the current pattern;

3. matcher (charsequeue input );

Generate a matcher object with a given name;

4. Static Boolean matchers (string RegEx, charsequeue intput );

Compile the given regular expression and expand the match for the input string in the regular expression mode. This method is suitable for the regular expression only once, that is, only one matching operation, in this case, you do not need to generate a matcher instance.

5. String Pattern ();

Returns the expression compiled by the pattern object.

6. String [] Split (charsequeue input );

Splits the target string into the pattern according to the regular expression in pattern.

String [] Split (charsequeue input, int limit); // limit indicates the number of specified segments.




Matcher class method:


1. matcher appendreplacement (stringbuffer Sb, string replacement );

Replace the current matched substring with the specified substring, and add the substring after the replacement and the string segment after the previous matched substring to a stringbuffer object.

2. stringbuffer appendtail (stringbuffer SB );

Add the remaining strings after the last match to a stringbuffer object.

3. Int end ();

Returns the index position of the original target string of the last matched substring.

Int end (INT group );

Returns the position of the last character of the matched substring in the matching mode.

4. boolean find ();

Try to find the next matched substring in the target string

Boolean find (INT start );

Reset the matcher object and try to find the next matched substring from the specified position in the target string.

5. String group ();

Returns the content of all substrings that match the Group obtained by the current query.

String group (INT group );

Returns the content of the substring that matches the specified group.

Int groupcount ();

Returns the number of matched groups in the current search.

6. boolean lookingat ();

Checks whether the target string starts with a matched substring.

7. boolean matches ();

The return value is returned only when the entire target string is fully matched.

8. pttern pattern ();

Returns the existing matching mode of the matcher object, that is, the corresponding pattern object.

9. String replaceall (string replacement );

Replace all substrings in the target string that match the existing mode with the specified string.

10. String replacefirst (string Repalcement );

Only Replace the first one .............

11. matcher reset () // reset the matcher object

Matcher reset () // reset the matcher object and create a new target string

12. Int start ();

Returns the position of the starting character of the substring in the original target string.

13. Int start (INT group );

Returns the position of the first character in the original target string of the substring that matches the specified group.

Regular Expressions: Pattern and mtcher

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.