橘子味的心
标题:Java正则表达式

Java提供了java.util.regex包,用于与正则表达式进行模式匹配。 Java正则表达式与Perl编程语言非常相似,非常容易学习。

正则表达式是一种特殊的字符序列,可使用模式中的专用语法来匹配或查找其他字符串或字符串集。 它们可用于搜索,编辑或操作文本和数据。

java.util.regex包主要由以下三个类组成 -

  • Pattern类 - Pattern对象是正则表达式的编译表示。 Pattern类不提供公共构造函数。 要创建模式,需要首先调用它的公共静态compile()方法,然后返回Pattern对象。 这些方法接受正则表达式作为第一个参数。

  • Matcher类 - Matcher对象是解释模式并对输入字符串执行匹配操作的引擎。 与Pattern类一样,Matcher没有定义公共构造函数。 通过在Pattern对象上调用matcher()方法获取Matcher对象。

  • PatternSyntaxException - PatternSyntaxException对象是未经检查的异常,指示正则表达式模式中的语法错误。

1. 捕获组

捕获组是将多个字符视为一个单元的一种方法。 它们是通过将要分组的字符放在一组括号中来创建的。 例如,正则表达式(dog)创建包含字母dog的单个组。

捕获组通过从左到右计算它们的左括号来编号。 在表达式((A)(B(C)))中,例如,有四个这样的组 -

  • ((A)(B(C)))
  • (A)
  • (B(C))
  • (C)

要查找表达式中存在多少个组,请在Matcher对象上调用groupCount()方法。 groupCount()方法返回一个int类型值,显示Matcher模式中存在的捕获组数。

还有一个特殊组,即组0,它始终代表整个表达式。 该组未包含在groupCount()报告的总数中。

示例

以下示例说明如何从给定的字母数字字符串中查找数字字符串 -

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class RegexMatches {
  5.  
  6. public static void main( String args[] ) {
  7. // String to be scanned to find the pattern.
  8. String line = "This order was placed for QT3000! OK?";
  9. String pattern = "(.*)(\\d+)(.*)";
  10.  
  11. // Create a Pattern object
  12. Pattern r = Pattern.compile(pattern);
  13.  
  14. // Now create matcher object.
  15. Matcher m = r.matcher(line);
  16. if (m.find( )) {
  17. System.out.println("Found value: " + m.group(0) );
  18. System.out.println("Found value: " + m.group(1) );
  19. System.out.println("Found value: " + m.group(2) );
  20. }else {
  21. System.out.println("NO MATCH");
  22. }
  23. }
  24. }
  25. Java

执行上面示例代码,得到以下结果:

  1. Found value: This order was placed for QT3000! OK?
  2. Found value: This order was placed for QT300
  3. Found value: 0
  4. Shell

2. 正则表达式语法

下面列出了Java中可用的所有正则表达式元字符语法 -

编号 子表达式 匹配
1 ^ 匹配行的开头。
2 $ 匹配行的结尾。
3 . 匹配除换行符之外的任何单个字符,使用m选项也可以匹配换行符。
4 [...] 匹配括号中的任何单个字符。
5 [^...] 匹配括号内的任何单个字符。
6 \A 整个字符串的开头。
7 \z 整个字符串的结尾。
8 \Z 除允许的最终行终止符之外的整个字符串的结尾。
9 re* 匹配前面表达式的0次或更多次出现。
10 re+ 匹配前面表达式的1次或更多次出现。
11 re? 匹配前面表达式的01次出现。
12 re{n} 准确匹配前面表达式的n次出现次数。
13 re{n,} 准确匹配前面表达式的n次以上出现次数。
14 aΙb 匹配ab
15 (re) 对正则表达式进行分组并记住匹配的文本。
16 (?: re) 将正则表达式分组而不记住匹配的文本。
17 (?> re) 匹配独立模式而无需回溯。
18 \w 匹配单词字符。
19 \W 匹配非单词字符。
20 \s 匹配空白符,相当于:[\t\n\r\f]
21 \S 匹配非空白。
22 \d 匹配数字,相当于:[0-9]
23 \D 匹配非数字。
24 \A 匹配字符串的开头。
25 \Z 匹配字符串的结尾。如果存在换行符,则它在换行符之前匹配。
26 \z 匹配字符串的结尾。
27 \G 匹配最后一个匹配结束的点。
28 \n 反向引用以捕获组号:n
29 \b 在括号外部匹配单词边界,在括号内匹配退格(0x08)。
30 \B 匹配非字边界。
31 \n,\t 匹配换行符,回车符,制表符等。
32 \E 转义(引用)所有字符直到\E
33 \Q 结束以\Q开头引用。

start()和end()方法

以下是计算字符串中:cat一词的出现次数示例 -

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class RegexMatches {
  5.  
  6. private static final String REGEX = "\\bcat\\b";
  7. private static final String INPUT = "cat cat cat cattie cat";
  8.  
  9. public static void main( String args[] ) {
  10. Pattern p = Pattern.compile(REGEX);
  11. Matcher m = p.matcher(INPUT); // get a matcher object
  12. int count = 0;
  13.  
  14. while(m.find()) {
  15. count++;
  16. System.out.println("Match number "+count);
  17. System.out.println("start(): "+m.start());
  18. System.out.println("end(): "+m.end());
  19. }
  20. }
  21. }
  22. Java

执行上面示例代码,得到以下结果:

  1. Match number 1
  2. start(): 0
  3. end(): 3
  4. Match number 2
  5. start(): 4
  6. end(): 7
  7. Match number 3
  8. start(): 8
  9. end(): 11
  10. Match number 4
  11. start(): 19
  12. end(): 22
  13. Shell

可以看到此示例使用单词边界来确保字母:c,a,t不仅仅是较长单词中的子字符串。 它还提供了有关输入字符串中匹配发生位置的一些有用信息。

start方法返回上一个匹配操作期间给定组捕获的子序列的起始索引,end返回匹配的最后一个字符的索引加1

matches和lookingAt方法

matches()lookingAt()方法都尝试将输入序列与模式匹配。 然而,不同之处在于匹配需要匹配整个输入序列,而查找则不需要。

两种方法总是从输入字符串的开头开始。 以下是上述方法的示例 -

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class RegexMatches {
  5.  
  6. private static final String REGEX = "foo";
  7. private static final String INPUT = "fooooooooooooooooo";
  8. private static Pattern pattern;
  9. private static Matcher matcher;
  10.  
  11. public static void main( String args[] ) {
  12. pattern = Pattern.compile(REGEX);
  13. matcher = pattern.matcher(INPUT);
  14.  
  15. System.out.println("Current REGEX is: "+REGEX);
  16. System.out.println("Current INPUT is: "+INPUT);
  17.  
  18. System.out.println("lookingAt(): "+matcher.lookingAt());
  19. System.out.println("matches(): "+matcher.matches());
  20. }
  21. }
  22. Java

执行上面示例代码,得到以下结果:

  1. Current REGEX is: foo
  2. Current INPUT is: fooooooooooooooooo
  3. lookingAt(): true
  4. matches(): false
  5. Shell

replaceFirst和replaceAll方法
replaceFirst()replaceAll()方法替换匹配给定正则表达式的文本。 正如其名称所示,replaceFirst()替换第一个匹配项,replaceAll()替换所有匹配项。

以下是上述功能的示例 -

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class RegexMatches {
  5.  
  6. private static String REGEX = "dog";
  7. private static String INPUT = "The dog says meow. " + "All dogs say meow.";
  8. private static String REPLACE = "cat";
  9.  
  10. public static void main(String[] args) {
  11. Pattern p = Pattern.compile(REGEX);
  12.  
  13. // get a matcher object
  14. Matcher m = p.matcher(INPUT);
  15. INPUT = m.replaceAll(REPLACE);
  16. System.out.println(INPUT);
  17. }
  18. }
  19. Java

执行上面示例代码,得到以下结果:

  1. The cat says meow. All cats say meow.
  2. Shell

appendReplacement和appendTail方法

Matcher类还提供了appendReplacementappendTail方法来替换文本。

以下是上述方法的示例 -

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class RegexMatches {
  5.  
  6. private static String REGEX = "a*b";
  7. private static String INPUT = "aabfooaabfooabfoob";
  8. private static String REPLACE = "-";
  9. public static void main(String[] args) {
  10.  
  11. Pattern p = Pattern.compile(REGEX);
  12.  
  13. // get a matcher object
  14. Matcher m = p.matcher(INPUT);
  15. StringBuffer sb = new StringBuffer();
  16. while(m.find()) {
  17. m.appendReplacement(sb, REPLACE);
  18. }
  19. m.appendTail(sb);
  20. System.out.println(sb.toString());
  21. }
  22. }
  23. Java

执行上面示例代码,得到以下结果:

  1. -foo-foo-foo-
  2. Shell