Java Regular Expression Summary
Package pack; import java. util. regex. matcher; import java. util. regex. pattern; public class Demo {public static void main (String [] args) {// method1 (); // match // method2 (); // cut // method3 (); // replace/* use the String Method */method4 ();} public static void method1 () {String qq = "1881265"; String regex1 = "[1-9] \ d {4, 14}"; sys (qq. matches (regex1); // whether the regex rule String s = "e"; // String regex = "[a-zA-Z]"; // letter/S Tring regex = "[a-d [m-p]"; // a to d or m to p/* \ d number [0-9] * \ D non-digit [^ 0-9] * \ s blank character * \ S not blank character * \ w word character [a-zA-Z0-9 _] * \ W non-word character *** X? One or zero ** zero or multiple times ** X + one or multiple times ** X {n} Exactly n times ** X {n,} At Least n times ** X {n, m} n to m times **/String regex = "\ d"; sys (s. matches (regex);} public static void method2 () {/* String s = "zhang li san"; String regex = "\ s + "; string [] arr = s. split (regex); for (String ss: arr) sys (ss); * // * String s = "zhang. li. san "; String regex = "\\. ";//\. represents. any Italian character ,\\. it is a common point and cannot be used directly. it is a special character String [] arr = s. split (regex); for (String ss: arr) sys (ss); * // * String s = "www \ abs \ 1.txt "; string regex = "\\\\"; String [] arr = s. split (regex); for (String ss: arr) sys (ss); */String s = "wekkyduopxxxs"; // cut by overlapping words/* cut by overlapping words, to make the rule result reusable, You can encapsulate the rule into a group and complete it with (). The group is numbered, starting from 1 */String regex = "(.) \ 1 + "; // parentheses are the concept of a group. \ 1 is a recurrence, and \ 1 + is a single or multiple String [] arr = s. split (regex); for (String ss: arr) sys (ss);} public static void method3 () {String s = "wj3374zns399nvn9999kl3333 "; // replace the number with # String regex = "[0-9]"; s. replaceAll (regex, "#"); sys (s);/* String s = "wekkyduopxxxs"; // Replace the stacked words with a single one, such: zzzz-> z String regex = "(.) \ 1 + "; s. replaceAll (regex, "$1"); sys (s); */} public static void method4 () {String s = "zhas asb cjcdj csj cbbdv "; string regex = "[a-z] {3}"; // encapsulate the rule into the object Pattern p = Pattern. compile (regex); // obtain the Matcher object m = p. matcher (s); sys (m. matches (); // match all, false. Note: if the pointer is moved backward, the following results will be affected: while (m. find () {sys (m. group (); // The matched string sys (m. start () + "... "+ m. end (); // the start and end bits of the matched string} public static void sys (Object obj) {System. out. println (obj );}}