Recently encountered with the Java Regular expression data input check check the Java JDK document, but some of the key words in the document really let the head big ah, this is not the key, the most painful is the explanation of the document is simple to make people crazy, so various search for information, finally some concepts understand
The Java Regular Expression tool consists of two classes, The Java.util.regex.Pattern pattern class and the Java.util.regex.Matcher match class, respectively, for the use of these classes can refer to the JDK documentation, here are some of the difficult to understand the key words
①.greedy,reluctant,possessive (greedy quantifier, reluctant quantifier, possessive quantifier), as opposed to the JDK document
Greedy is greedy, reluctant is reluctant, also can be understood as non-greedy non-greedy, possessive is exclusive, possessive, these keywords only in the Java regular expression of quantifier expression function. such as {n,m},{n,m}?,{n,m}+, a greedy, non-greedy, exclusive matching algorithm
Greedy algorithm: On the premise that the whole regular expression match succeeds, the character part of Quantifier modification will match the string as much as possible, if the whole regular expression match is unsuccessful because the quantifier partial match is too many, the algorithm will step back the quantifier matching string part to make the whole regular expression match successfully.
Reluctant algorithm: With the success of the entire regular expression matching, the quantifier-modified character part matches the string as little as possible.
Possessive algorithm: Similar to the greedy algorithm, but the algorithm once the match is unsuccessful will not be gradual fallback. The algorithm is also used to modify the quantifier
The expression part matches as many strings as possible, but the algorithm regardless of whether the entire regular expression succeeds, once the quantifier after the regular expression part does not match the string, the match will fail, and will not fallback, but immediately end the match
eg.
Public classREGEXP { Public Static voidMain (string[] args) {String str= "aaa4bbb6cc"; String RegExp= ". {3,10}\\d "; Pattern Pattern=Pattern.compile (REGEXP); Matcher Matcher=Pattern.matcher (str); if(Matcher.find ()) {System.out.println ("Match string:" + matcher.group () + "\tmatch start:" + matcher.start () + "\tmatch End:" +matcher.end ()); } Else{System.out.println ("Not match"); } }}
Run Result: match String:aaa4bbb6 match start:0 match End:8
Then change the quantifier section {3,10} to the reluctant quantifier as follows
public class RegExp {public static void Main (string[] args) { String str = "aaa4bbb6cc"; String regExp = ". {3,10}?\\d "; Pattern pattern = pattern.compile (REGEXP); Matcher Matcher = Pattern.matcher (str); if (Matcher.find ()) { System.out.println ("Match string:" + matcher.group () + "\tmatch start:" + matcher.start () + "\tm Atch End: "+ matcher.end ()); } else { System.out.println ("not Match");}}}
Run Result: match string:aaa4 match start:0 match End:4
Finally, the match quantifier is changed to {3,10}+possessive quantifier as follows
1 Public classREGEXP {2 3 Public Static voidMain (string[] args) {4 5String str = "aaa4bbb6cc";6String regExp = ". {3,10}+\\d ";7Pattern pattern =Pattern.compile (REGEXP);8Matcher Matcher =Pattern.matcher (str);9 if(Matcher.find ()) {TenSystem.out.println ("Match string:" + matcher.group () + "\tmatch start:" + matcher.start () + "\tmatch End:" +matcher.end ()); One}Else { ASystem.out.println ("not match"); - } - } the}
Run Result: not match
Run the result analysis and non-capturing group tomorrow to continue ... Go to bed...
Java Regular Expressions