一、Java中的正则API

1 基本介绍

1.1 正则相关类

java.util.regex 包主要包括以下三个类:

  • Pattern 类:pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。
  • Matcher 类:Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。
  • PatternSyntaxException:PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

    1.2 Java中的转义

    在其他语言中,\ 表示:我想要在正则表达式中插入一个普通的(字面上的)反斜杠,请不要给它任何特殊的意义。
    在 Java 中,\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。
    所以,在其他的语言中(如 Perl),一个反斜杠 \ 就足以具有转义的作用,而在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也可以简单的理解在 Java 的正则表达式中,两个 \ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \d,而表示一个普通的反斜杠是 \。

1.3 代码示例

1.3.1 示例1

  1. public static void main(String[] args){
  2. String content = "I am noob " +
  3. "from runoob.com.";
  4. String pattern = ".*runoob.*";
  5. boolean isMatch = Pattern.matches(pattern, content);
  6. System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
  7. }

执行结果: 字符串中是否包含了 ‘runoob’ 子字符串? true

1.3.2 示例2

  1. public static void main( String[] args ){
  2. // 按指定模式在字符串查找
  3. String line = "This order was placed for QT3000! OK?";
  4. String pattern = "(\\D*)(\\d+)(.*)";
  5. // 创建 Pattern 对象
  6. Pattern r = Pattern.compile(pattern);
  7. // 现在创建 matcher 对象
  8. Matcher m = r.matcher(line);
  9. if (m.find( )) {
  10. System.out.println("Found value: " + m.group(0) );
  11. System.out.println("Found value: " + m.group(1) );
  12. System.out.println("Found value: " + m.group(2) );
  13. System.out.println("Found value: " + m.group(3) );
  14. } else {
  15. System.out.println("NO MATCH");
  16. }
  17. }

执行结果: Found value: This order was placed for QT3000! OK? Found value: This order was placed for QT Found value: 3000 Found value: ! OK?

2 Matcher类

2.1 相关API

名称 说明
find() 返回目标字符串中是否包含与 Pattern 匹配的子串
group() 返回上一次与 Pattern 匹配的子串
start() 返回上一次与 Pattern 匹配的子串在目标字符串中的开始位置
end() 返回上一次与 Pattern 匹配的子串在目标字符串中的结束位置加 1
lookingAt() 返回目标字符串前面部分与 Pattern 是否匹配
matches() 返回整个目标字符串与 Pattern 是否匹配
reset() 将现有的 Matcher 对象应用于一个新的字符序列。

2.2 示例代码

2.2.1 示例1

  1. public static void main( String[] args ){
  2. // 按指定模式在字符串查找
  3. String line = "This order was placed for QT3000! OK?";
  4. String pattern = "[a-zA-Z]+\\s";
  5. // 创建 Pattern 对象
  6. Pattern r = Pattern.compile(pattern);
  7. // 现在创建 matcher 对象
  8. Matcher m = r.matcher(line);
  9. int count = 0;
  10. while (m.find( )) {
  11. count++;
  12. System.out.println(count+"start:"+m.start());
  13. System.out.println(count+"end:"+m.end());
  14. System.out.println(count+"匹配的子串:"+m.group());
  15. }
  16. }

执行结果: 1start:0 1end:5 1匹配的子串:This 2start:5 2end:11 2匹配的子串:order 3start:11 3end:15 3匹配的子串:was 4start:15 4end:22 4匹配的子串:placed 5start:22 5end:26 5匹配的子串:for

2.2.2 示例2

  1. private static final String REGEX = "foo";
  2. private static final String INPUT = "fooooooooooooooooo";
  3. private static final String INPUT2 = "ooooofoooooooooooo";
  4. private static Pattern pattern;
  5. private static Matcher matcher;
  6. private static Matcher matcher2;
  7. public static void main( String[] args ){
  8. pattern = Pattern.compile(REGEX);
  9. matcher = pattern.matcher(INPUT);
  10. matcher2 = pattern.matcher(INPUT2);
  11. System.out.println("Current REGEX is: "+REGEX);
  12. System.out.println("Current INPUT is: "+INPUT);
  13. System.out.println("Current INPUT2 is: "+INPUT2);
  14. System.out.println("lookingAt(): "+matcher.lookingAt());
  15. System.out.println("matches(): "+matcher.matches());
  16. System.out.println("lookingAt(): "+matcher2.lookingAt());
  17. }

执行结果: Current REGEX is: foo
Current INPUT is: fooooooooooooooooo
Current INPUT2 is: ooooofoooooooooooo
lookingAt(): true
matches(): false
lookingAt(): false