Java Regular expression pattern and Matcher class

Source: Internet
Author: User

reproduced from-- small Fish is bad (original link)

Overview

The function of the pattern class is to create a matching pattern after the regular expression is compiled.
The Matcher class matches regular expressions using the pattern information provided by the pattern instance

Pattern class
Common methods and introduction

1. Pattern Complie (String regex)
Because the constructor of the pattern is private and cannot be created directly, it is created by means of the static method compile (String regex) method, and the given regular table

Compiled and assigned to the pattern class

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

Cases
String regex = "\\?| \\*";
Pattern pattern = pattern.compile (regex);
String patternstr = Pattern.pattern ();//Return \?\*


3. The Pattern compile (string regex, int flags) method functions the same as compile (string regex), but adds the flag parameter

the int flags () returns the matching flag parameter of the current pattern.
The flag parameter is used to control the matching behavior of the regular expression, and the range of values is as follows:

Pattern.canon_eq A match is determined only if the "normal decomposition (canonical decomposition)" of the two characters is identical. For example,
after using this flag, the expression "a\u030a" will match "?". By default, "canonical equality (canonical equivalence)" is not considered.

pattern.case_insensitive (? i) by default, case-insensitive matching applies only to the US-ASCII character set. This flag allows the expression to ignore the size
write to match. To match a Unicode character to an unknown size, just combine the unicode_case with this flag.

pattern.comments (? x) in this mode, the match is ignored (in regular expression) empty characters (translator Note: Not refers to the expression "\s", but refers to
the space in the Expression, tab, enter, and so on). Comments start with # until the end of the line. You can enable UNIX line mode by using an embedded flag.

Pattern.dotall (? s) in this mode, the expression '. ' You can match any character, including the Terminator that represents a line. By default, the expression '. ' No
matches the Terminator of the row.

pattern.multiline (? m) in this mode, ' \^ ' and ' $ ' match the start and end of a line, respectively. Also, ' ^ ' still matches the beginning of the string, ' $ '
also matches the end of the string. By default, these two expressions match only the beginning and end of a string.

pattern.unicode_case (? u) in this mode, if you also enable the Case_insensitive flag, then it will be case-insensitive to UNICODE characters
a sense of matching. By default, case-insensitive matches apply only to the Us-ascii character set.

pattern.unix_lines (? d) in this mode, only ' \ n ' is considered a line abort and is matched with '. ', ' ^ ', and ' $ '.


4. Pattern.matcher (charsequence input) creates a Matcher object for the specified input string

Cases
Pattern pattern = Pattern.compile ("\\?{ 2} ");
Matcher Matcher = Pattern.matcher ("??");
Boolean matches = Matcher.matches ();//True


5. string[] Split (charsequence input)
String[] Split (charsequence input, int limit)
function and string[] split (charsequence input), add parameter limit to specify the number of segments to be split

Cases
String regex = ",";
Pattern pattern = pattern.compile (regex);
//must be in array form, because the split is saved by default after split
string[] Splitstrs = Pattern.split ("123,123,456,456");//123 123 456 456
string[] splitStrs2 = Pattern.split ("123,123,456,456", 2);//123 123,456,456



6. Pattern.quote (string s) returns the literal of the given string, please refer to 123 for specific information about the method.
Cases
String pattern = pattern.quote ("1252343% 8 567 HDFG gf^$545");
System.out.println ("Pattern is:" +pattern);

Output information:
Pattern is: \q1252343% 8 567 HDFG gf^$545\e


7. The matches () method compiles the given regular expression and matches the input string with the regular expression, which is appropriate for the case where the regular expression is used only once, that is, only one match is performed, because in this case there is no need to generate an Matcher instance.

Cases
String regex = "\\?| \\*";
Pattern pattern = pattern.compile (regex);
Boolean matches = Pattern.matches (regex, "?"); /returns True


*******************************************
Matcher class
Common methods and introduction

1. Boolean matches () the most common method: try to expand the match detection for the entire target character, that is, the true value is returned only if the entire target string matches exactly.
Cases
Pattern pattern = Pattern.compile ("\\?{ 2} ");
Matcher Matcher = Pattern.matcher ("??");
Boolean matches = Matcher.matches ();//true
System.out.println (matches);
Matcher=pattern.matcher ("?");
matches = Matcher.matches ();//false
System.out.println (matches);



2. Boolean Lookingat () matches the preceding string, and only the string that matches to the front will return true
Cases
Pattern p = pattern.compile ("\\d+");
Matcher m = P.matcher ("22bb23");
Boolean match = M.lookingat ();//true
System.out.println (match);
m = P.matcher ("bb2233");
Match= M.lookingat ();
System.out.println (match);//false



3. Boolean find () matches the string, and the matching string can be anywhere
Cases
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 ();//return False


4. int start () returns the position of the currently matched string in the original target string
int end () returns the index position of the last character of the currently matched string in the original target string.
String Group () returns the substring matched to
Pattern.start (), Pattern.end (), Pattern.group () code example
Note: The start (), End (), and group () methods must be placed behind the Find () Method!!!

Cases
Pattern p=pattern.compile ("Wo");
Matcher m=p.matcher ("Hello,world");
M.find ();//must be there, otherwise the start () and group () methods will be error-
System.out.println (M.start ());//6
System.out.println (M.group ());//Wo

Java Regular expression pattern and Matcher class

Related Article

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.