Java基础 专题
专题目录
您的位置:java > Java基础专题 > Java正则表达式
Java正则表达式
作者:--    发布时间:2019-11-20

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()报告的总数中。

示例

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

import java.util.regex.matcher;
import java.util.regex.pattern;

public class regexmatches {

   public static void main( string args[] ) {
      // string to be scanned to find the pattern.
      string line = "this order was placed for qt3000! ok?";
      string pattern = "(.*)(\\d+)(.*)";

      // create a pattern object
      pattern r = pattern.compile(pattern);

      // now create matcher object.
      matcher m = r.matcher(line);
      if (m.find( )) {
         system.out.println("found value: " + m.group(0) );
         system.out.println("found value: " + m.group(1) );
         system.out.println("found value: " + m.group(2) );
      }else {
         system.out.println("no match");
      }
   }
}

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

found value: this order was placed for qt3000! ok?
found value: this order was placed for qt300
found value: 0

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一词的出现次数示例 -

import java.util.regex.matcher;
import java.util.regex.pattern;

public class regexmatches {

   private static final string regex = "\\bcat\\b";
   private static final string input = "cat cat cat cattie cat";

   public static void main( string args[] ) {
      pattern p = pattern.compile(regex);
      matcher m = p.matcher(input);   // get a matcher object
      int count = 0;

      while(m.find()) {
         count++;
         system.out.println("match number "+count);
         system.out.println("start(): "+m.start());
         system.out.println("end(): "+m.end());
      }
   }
}

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

match number 1
start(): 0
end(): 3
match number 2
start(): 4
end(): 7
match number 3
start(): 8
end(): 11
match number 4
start(): 19
end(): 22

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

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

matches和lookingat方法

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

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

import java.util.regex.matcher;
import java.util.regex.pattern;

public class regexmatches {

   private static final string regex = "foo";
   private static final string input = "fooooooooooooooooo";
   private static pattern pattern;
   private static matcher matcher;

   public static void main( string args[] ) {
      pattern = pattern.compile(regex);
      matcher = pattern.matcher(input);

      system.out.println("current regex is: "+regex);
      system.out.println("current input is: "+input);

      system.out.println("lookingat(): "+matcher.lookingat());
      system.out.println("matches(): "+matcher.matches());
   }
}

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

current regex is: foo
current input is: fooooooooooooooooo
lookingat(): true
matches(): false

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

以下是上述功能的示例 -

import java.util.regex.matcher;
import java.util.regex.pattern;

public class regexmatches {

   private static string regex = "dog";
   private static string input = "the dog says meow. " + "all dogs say meow.";
   private static string replace = "cat";

   public static void main(string[] args) {
      pattern p = pattern.compile(regex);

      // get a matcher object
      matcher m = p.matcher(input); 
      input = m.replaceall(replace);
      system.out.println(input);
   }
}

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

the cat says meow. all cats say meow.

appendreplacement和appendtail方法

matcher类还提供了appendreplacementappendtail方法来替换文本。

以下是上述方法的示例 -

import java.util.regex.matcher;
import java.util.regex.pattern;

public class regexmatches {

   private static string regex = "a*b";
   private static string input = "aabfooaabfooabfoob";
   private static string replace = "-";
   public static void main(string[] args) {

      pattern p = pattern.compile(regex);

      // get a matcher object
      matcher m = p.matcher(input);
      stringbuffer sb = new stringbuffer();
      while(m.find()) {
         m.appendreplacement(sb, replace);
      }
      m.appendtail(sb);
      system.out.println(sb.tostring());
   }
}

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

-foo-foo-foo-

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册