1 Packagetest1;2 3 ImportJava.util.regex.Matcher;4 ImportJava.util.regex.Pattern;5 6 Public classTestexp {7 /**8 * 9 * When using regular expressions, we often use () to enclose a part, called a sub-pattern. There are two cases of capturing and non-capturing in the sub-mode. Ten * Capturing means get match: One * means that the system will save all sub-pattern matching results behind the scenes for us to find or replace them. Use such as a back reference (applying the preceding matched substring after the expression); A * Non-capturing refers to a non-acquisition match: - * At this time the system does not save sub-pattern matching results, sub-pattern matching is only used as a restrictive condition, such as forward pre-check, reverse pre-check, negative forward pre-check, negative reverse pre-check and so on. - * the * You can use non-fetch matches as criteria in Java to filter substrings. For example, to get "Hello World!" "Hello World" in the - *, note, do not exclamation mark. A non-fetching regular expression is used when the exclamation mark can be matched again. ("Hello World (? =!)" ) - * - * Use of back references in Java: + * "ABC def". Replacefirst ("(\\w+) \\s+ (\\w+)", "$ $ $");//result for DEF ABC - * "ABC def AAA BBB". ReplaceAll ("(\\w+) \\s+ (\\w+)", "$ $ $");//result is def ABC BBB AAA + * A * Use $x to replace character neutron strings. at * - * */ - Public Static voidMain (string[] args) { - /**The subscript for GroupCount starts at 0, which means that if you get a match, the value is 0 .*/ - /**positive pre-check*/ - /** in * Purpose: To find a substring in a meta-character that matches the "win" format, and the string immediately follows a "7" string. - * to * */ + /* - * Pattern PTN = Pattern.compile ("Win (? =7)", pattern.unicode_case); the * Matcher m = ptn.matcher ("Win 7"); System.out.println ("GroupCount:" * * + m.groupcount ()); while (M.find ()) {System.out.println (M.group ());} $ */Panax Notoginseng /**positive negative pre-check*/ - /** the * Purpose: To find a substring in a meta-character that matches the "win" format, and the string is not followed by a "7" string. + * A * */ the /* + * Pattern PTN = Pattern.compile ("Win (?! 7) ", pattern.unicode_case); - * Matcher m = ptn.matcher ("Win 8"); while (M.find ()) { $ * SYSTEM.OUT.PRINTLN (M.group ());} $ */ - /**Reverse positive pre-check*/ - /** the * Purpose: Finds the substring in the meta string that matches "win" and follows the string "7" in front of it. Win is obtained because 7 is not acquired. - * Wuyi * */ the /* - * Pattern PTN = Pattern.compile ("(? <=7) win", Pattern.unicode_case); Wu * Matcher m = ptn.matcher ("7win"); while (M.find ()) { - * SYSTEM.OUT.PRINTLN (M.group ());} About */ $ /**Reverse negation pre-check*/ - /** - * Purpose: Finds substrings in a meta string that match "win", and does not precede the string with "7". - * A */ + /* the * Pattern PTN = Pattern.compile ("(? <!7) win", Pattern.unicode_case); - * Matcher m = ptn.matcher ("8win"); while (M.find ()) { $ * SYSTEM.OUT.PRINTLN (M.group ());} the */ the } the}
Java regular expressions get matching and non-fetch matches