No |
正则表达式
|
描述 |
1 | . |
匹配任何字符
|
2 | ^regex |
找到必须在行头的匹配的正则
|
3 | regex$ |
找到必须在行的末尾匹配的正则
|
4 | [abc] |
集定义,可以匹配字母a或b或c
|
5 | [abc][vz] |
集定义,可以匹配a或b或c后跟v或z
|
6 | [^abc] | 当插入符显示为方括号中的第一个字符,它为相反模式。这可以匹配除了a或b或c外的任何字符 |
7 | [a-d1-7] |
范围:从1到7、a和d之间的字符
|
8 | X|Z |
查找X或Z
|
9 | XZ |
查找X直接跟着Z
|
10 | $ |
检查一行是否结束
|
|
||
11 | \d | 任何数字, [0-9]的简写 |
12 | \D | 非数字, [^0-9]的简写 |
13 | \s | 空白字符, [ \t\n\x0b\r\f]的简写 |
14 | \S | 非空格字符, [^\s]的简写 |
15 | \w | 单词字符, [a-zA-Z_0-9]的简写 |
16 | \W | 非单词字符 [^\w] |
17 | \S+ |
一些非空白字符
|
18 | \b | 匹配一个单词边界,在一个字字符 [a-zA-Z0-9_]. |
|
||
19 | * | 出现零次或多次, {0,}的简写 |
20 | + | 出现一次或更多次, {1,}的简写 |
21 | ? | 没有出现次或一次? {0,1}.的简写 |
22 | {X} |
出现 x 次,{}描述了前自由的顺序
|
23 | {X,Y} |
X和Y之间出现的次数
|
24 | *? | ? 后一个量词使其成为一个勉强的限定符。它试图找到最小匹配。 |
\.[{(*+?^$|
// Regex pattern describe any character. String regex = "."; // Regex pattern describe a dot character. String regex = "\\.";
... // Check the entire String object matches the regex or not. public boolean matches(String regex) ..
package com.yiibai.tutorial.regex.stringmatches; public class StringMatches { public static void main(String[] args) { String s1 = "a"; System.out.println("s1=" + s1); // Check the entire s1 // Match any character // Rule . // ==> true boolean match = s1.matches("."); System.out.println("-Match . " + match); s1 = "abc"; System.out.println("s1=" + s1); // Check the entire s1 // Match any character // Rule . // ==> false (Because s1 has three characters) match = s1.matches("."); System.out.println("-Match . " + match); // Check the entire s1 // Match with any character 0 or more times // Combine the rules . and * // ==> true match = s1.matches(".*"); System.out.println("-Match .* " + match); String s2 = "m"; System.out.println("s2=" + s2); // Check the entire s2 // Start by m // Rule ^ // ==> true match = s2.matches("^m"); System.out.println("-Match ^m " + match); s2 = "mnp"; System.out.println("s2=" + s2); // Check the entire s2 // Start by m // Rule ^ // ==> false (Because s2 has three characters) match = s2.matches("^m"); System.out.println("-Match ^m " + match); // Start by m // Next any character, appearing one or more times. // Rule ^ and. and + // ==> true match = s2.matches("^m.+"); System.out.println("-Match ^m.+ " + match); String s3 = "p"; System.out.println("s3=" + s3); // Check s3 ending with p // Rule $ // ==> true match = s3.matches("p$"); System.out.println("-Match p$ " + match); s3 = "2nnp"; System.out.println("s3=" + s3); // Check the entire s3 // End of p // ==> false (Because s3 has 4 characters) match = s3.matches("p$"); System.out.println("-Match p$ " + match); // Check out the entire s3 // Any character appearing once. // Followed by n, appear one or up to three times. // End by p: p $ // Combine the rules: . , {X, y}, $ // ==> true match = s3.matches(".n{1,3}p$"); System.out.println("-Match .n{1,3}p$ " + match); String s4 = "2ybcd"; System.out.println("s4=" + s4); // Start by 2 // Next x or y or z // Followed by any one or more times. // Combine the rules: [abc]. , + // ==> true match = s4.matches("2[xyz].+"); System.out.println("-Match 2[xyz].+ " + match); String s5 = "2bkbv"; // Start any one or more times // Followed by a or b, or c: [abc] // Next z or v: [zv] // Followed by any // ==> true match = s5.matches(".+[abc][zv].*"); System.out.println("-Match .+[abc][zv].* " + match); } }
package com.yiibai.tutorial.regex.stringmatches; public class SplitWithRegex { public static final String TEXT = "This is my text"; public static void main(String[] args) { System.out.println("TEXT=" + TEXT); // White space appears one or more times. // The whitespace characters: \t \n \x0b \r \f // Combining rules: \ s and + String regex = "\\s+"; String[] splitString = TEXT.split(regex); // 4 System.out.println(splitString.length); for (String string : splitString) { System.out.println(string); } // Replace all whitespace with tabs String newText = TEXT.replaceAll("\\s+", "\t"); System.out.println("New text=" + newText); } }
package com.yiibai.tutorial.regex.stringmatches; public class EitherOrCheck { public static void main(String[] args) { String s = "The film Tom and Jerry!"; // Check the whole s // Begin by any characters appear 0 or more times // Next Tom or Jerry // End with any characters appear 0 or more times // Combine the rules:., *, X | Z // ==> true boolean match = s.matches(".*(Tom|Jerry).*"); System.out.println("s=" + s); System.out.println("-Match .*(Tom|Jerry).* " + match); s = "The cat"; // ==> false match = s.matches(".*(Tom|Jerry).*"); System.out.println("s=" + s); System.out.println("-Match .*(Tom|Jerry).* " + match); s = "The Tom cat"; // ==> true match = s.matches(".*(Tom|Jerry).*"); System.out.println("s=" + s); System.out.println("-Match .*(Tom|Jerry).* " + match); } }
String regex= ".xx."; // Create a Pattern object through a static method. Pattern pattern = Pattern.compile(regex); // Get a Matcher object Matcher matcher = pattern.matcher("MxxY"); boolean match = matcher.matches(); System.out.println("Match "+ match);
public static Pattern compile(String regex, int flags) ; public static Pattern compile(String regex); public Matcher matcher(CharSequence input); public static boolean matches(String regex, CharSequence input);
public int start() public int start(int group) public int end() public int end(int group) public String group() public String group(int group) public String group(String name) public int groupCount() public boolean matches() public boolean lookingAt() public boolean find()
package com.yiibai.tutorial.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherFind { public static void main(String[] args) { final String TEXT = "This \t is a \t\t\t String"; // Spaces appears one or more time. String regex = "\\s+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(TEXT); int i = 0; while (matcher.find()) { System.out.print("start" + i + " = " + matcher.start()); System.out.print(" end" + i + " = " + matcher.end()); System.out.println(" group" + i + " = " + matcher.group()); i++; } } }
package com.yiibai.tutorial.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherLookingAt { public static void main(String[] args) { String country1 = "iran"; String country2 = "Iraq"; // Start by I followed by any character. // Following is the letter a or e. String regex = "^I.[ae]"; Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(country1); // lookingAt () searches that match the first part. System.out.println("lookingAt = " + matcher.lookingAt()); // matches() must be matching the entire System.out.println("matches = " + matcher.matches()); // Reset matcher with new text: country2 matcher.reset(country2); System.out.println("lookingAt = " + matcher.lookingAt()); System.out.println("matches = " + matcher.matches()); } }
// A regular expression String regex = "\\s+=\\d+"; // Writing as three group, by marking () String regex2 = "(\\s+)(=)(\\d+)"; // Two group String regex3 = "(\\s+)(=\\d+)";
从Java7,您可以定义一个名为捕获组(?<名称>模式),并且您可以访问Matcher.group(字符串名称)匹配的内容。正则表达式较长,但代码更有意义,因为它表明您要匹配,或者使用正则表达式提取。注意:使用(?:模式),告知Java不要认为这是一个组(非捕获组)
package com.yiibai.tutorial.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class NamedGroup { public static void main(String[] args) { final String TEXT = " int a = 100;float b= 130;float c= 110 ; "; // Use (?pattern) to define a group named: groupName // Defined group named declare: using (? ...) // And a group named value: use: (? ..) String regex = "(? \\s*(int|float)\\s+[a-z]\\s*)=(? \\s*\\d+\\s*);"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(TEXT); while (matcher.find()) { String group = matcher.group(); System.out.println(group); System.out.println("declare: " + matcher.group("declare")); System.out.println("value: " + matcher.group("value")); System.out.println("------------------------------"); } } }
// This is a regex // any characters appear 0 or more times, // followed by ' and > String regex = ".*'>"; // TEXT1 match the regex. String TEXT1 = "FILE1'>"; // And TEXT2 match the regex String TEXT2 = "FILE1'> ";
package com.yiibai.tutorial.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class NamedGroup2 { public static void main(String[] args) { String TEXT = "File 1" + "File 2"; // Java >= 7. // Define group named fileName. // *? ==> ? after a quantifier makes it a reluctant quantifier. // It tries to find the smallest match. String regex = "/file/(?.*?)'>"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(TEXT); while (matcher.find()) { System.out.println("File Name = " + matcher.group("fileName")); } } }