These two days recalled the use of regular expressions, and by the way summed up the use of regular expressions in Java JavaScript, you need to see the use of regular friends in JavaScript can see my other summary, below I will briefly introduce the use of the Java Express expression. Convenient to use later query, but also hope to help everyone. = = = Welcome
In JDK1.3 and previous JDK versions, there are no classes that contain regular expressions, and if you want to use regular expressions in Java, you must use a regular expression library provided by a third-party. Support for the regular expression API, which is located in the Java.util.regex package, is provided starting from JDK1.4.
First, the introduction of common symbols
1.1 Predefined character classes
| Symbol |
Description |
| . |
Any character (may or may not match the line terminator) |
| \d |
Numbers:[0-9] |
| \d |
Non-numeric: [^0-9] |
| \s |
whitespace characters:[\t\n\x0b\f\r] |
| \s |
Non-whitespace characters:[^\s] |
| \w |
Word character:[a-za-z_0-9] |
| \w |
Non-word characters:[^\w] |
| \ |
Escape characters, such as "\ \" matches "\", "\{" matches "{". |
1.2 Quantity words
| Symbol |
Description |
| * |
Equivalent to {0,}
Match 0 to more characters before it. For example, the regular expression "zo*" can Match "Z" and "Zoo"; the regular expression ". *" means that you can match any string. |
| + |
Equivalent to {1,}
Matches the preceding subexpression one or more times. For example, the regular expression + + matches 9, 99, 999, and so on. |
| ? |
Equivalent to {0,1}
Matches the preceding subexpression 0 or one time. For example, "Do (es)?" can match "do" in "do" or "does". There is another use for this meta-character, which is to represent a non-greedy pattern match, followed by an introduction |
| {n} |
Matches the determined n times. For example, "e{2}" cannot match "D" in "bed", but can match two "E" in "seed". |
| {n,} |
Match at least N times. For example, "e{2,}" cannot match "E" in "bed", but can match all "E" in "Seeeeeeeed". |
| {n,m} |
Matches at least N times and matches up to M times. "e{1,3}" will match the first three "E" in "Seeeeeeeed". |
1.3 Boundary Match Symbol
| Symbol |
Description |
| ^ |
The beginning of the line |
| $ |
End of Line |
| \b |
Word boundaries |
| \b |
Non-word boundary |
| \a |
Start of input |
| \g |
End of last match |
| \z |
The end of the input, only for the last terminator (if any) |
| \z |
End of input |
1.3 Other common symbols
| [] The use of--or |
Description |
| [] |
Match any one of the characters in parentheses |
| [ABC] |
a,b or C(Simple Class) |
| [^ABC] |
Any character except a,b , or C(negation) |
| [A-za-z] |
a to z or a to z, the letters at both ends are included (range) |
| [A-d[m-p]] |
a to D or m to p:[a-dm-p](set) |
| [A-z&&[def]] |
d,e or F(intersection) |
| [A-Z&&[^BC]] |
a to Z, except for b and C:[ad-z](minus) |
| [A-z&&[^m-p]] |
a to z, not m to p:[a-lq-z](minus) |
| use of ()--group |
|
| () |
The expression that is enclosed in () is defined as a group, andthe character that matches the expression is saved to a staging area, which is useful when the string is extracted. Capturing groups can be numbered by calculating their opening brackets from left to right. |
| (\d) |
First Group |
| ( (A) (B (C))) |
Group I ((a) (b)) Group II (a) cluster III (b (c)) group fourth (c) |
PS: More Other symbols, refer to http://www.cnblogs.com/Mustr/p/6057159.html
Second, the common operation
2.1 Matches
String matches () method. Matches the entire string with a rule, so long as there is a non-conforming rule, the match ends and false is returned.
Example:
Public Static void checkqq () { = "123a45664"; = "[1-9]\\d{4,14}"; boolean flag = qq.matches (regex); if (flag) SYSTEM.OUT.PRINTLN (QQ+ "... is ok"); Else System.out.println (QQ+ "... Not valid "); } //Not legal
/* Match phone number segment only 13xxx 15xxx 18xxxx */public staticvoid Checktel () { = "16900001111"; = "1[358]\\d{9}"; System.out.println (Tel.matches (Telreg)); }
2.2 Cutting
String split () method; Splits this string according to the match of the given regular expression. Returns an array.
Example:
Public staticvoid Splitdemo () { = "avg bb Geig GLSD abc "; = " +"; // Cut by multiple spaces string[] arr = str.split (reg); System.out.println (arr.length); for (String s:arr) { System.out.println (s); } }
Examples of groups group:
Public Static voidSplitdemo () {String str= "Erkktyqqquizzzzzo"; String Reg="(.) \\1+ ";//cut according to the overlapping words//you can encapsulate a rule into a group. complete with (). The appearance of a group is numbered. //starting from 1. If you want to use an existing group, you can get it in the form of \ n (the number of the group). string[] arr =Str.split (REG); System.out.println (arr.length); for(String S:arr) {System.out.println (s); } } //Er,ty,ui,o
2.3 Replacement
String ReplaceAll (regex,str) method; Replaces this string with the given replacement for all substrings that match the given regular expression.
PS: If there is a defined group in the regex, you can get the existing group in the regular expression by the $ symbol in the second argument.
Example:
Public Static void Replacealldemo () { = "wer1389980000ty1234564uiod234345675f"; // Replace the number in the string with #. = Str.replaceall ("\\d{5,}", "#"); System.out.println (str); } // wer#ty#uio#f
Examples of groups group:
Public Static void Replacealldemo () { = "Erkktyqqquizzzzzo"; // replace the overlapping words with $. // replaces overlapping characters with a single letter. Zzzz->z = Str.replaceall("(. ) \\1+ "," $ "); System.out.println (str); } // Erktyquizo
2.4 Get
----take out the rules-compliant substrings in the string.
Operation Steps:
1, encapsulates the regular expression into an object.
2 to associate the regular object with the string to manipulate.
3, after correlation, gets the regular match engine.
4, by the engine to the rules of the sub-string operation, such as remove.
Example:
Public Static voidGetdemo () {String str= "Yin yu shi wo zui cai de yu Yan"; System.out.println (str); String Reg= "\\b[a-z]{3}\\b";//match a word with only three letters//encapsulates a rule as an object. Pattern p =Pattern.compile (REG); //associates the regular object with the string to be played. Gets the match object. Matcher m =P.matcher (str); //System.out.println (M.matches ());//in fact, the matches method in the string class. The pattern and Matcher objects are used to accomplish this. //It is simple to use when the string method is encapsulated. But the functions are single. //Boolean B = M.find ();//The rule is scoped to the string and the substring is found that conforms to the rule. //System.out.println (b); //System.out.println (M.group ());//used to get the result after a match. while(M.find ()) {System.out.println (M.group ()); System.out.println (M.start ()+"...."+m.end ()); //Start () character starting subscript (included)//End () character ending subscript (not included) } }
Iii. Summary
What is the use of the method??????
Way of thinking:
1, if you just want to know if the character is right or wrong, use match.
2, you want to change the existing string to another string, replace it.
3, you want to change the string into multiple strings in a custom way. Cutting. Gets a substring other than the rule.
4, to get the string substring that meets the requirements. Gets the substring that conforms to the rule.
The above content is a common way of summary, want to use good Java regular, more to study pattern and matcher two objects,. It's all under the Java.util.regex bag.
======== Welcome to =========
Use of regular expressions in Java (common methods)