正则表达式匹配忽略大小写

正则表达式匹配时有两种忽略大小写的方式: (?i) 之后的字符在匹配时忽略大小写Patter.compile() 时,指定忽略大小写模式 1、(?i) 之后

正则表达式匹配时有两种忽略大小写的方式:

  • (?i) 之后的字符在匹配时忽略大小写
  • Patter.compile() 时,指定忽略大小写模式
1、(?i) 之后的字符在匹配时忽略大小写。

比如:

        System.out.println(Pattern.compile("(?i)abc").matcher("ABC").matches()); // trueSystem.out.println(Pattern.compile("a(?i)bc").matcher("ABC").matches()); // falseSystem.out.println(Pattern.compile("a(?i)bc").matcher("aBC").matches()); // true

其中,(?i) 可以在起始标识 ^ 之前:

        System.out.println(Pattern.compile("(?i)^abc$").matcher("ABC").matches()); // true
2、Patter.compile() 时,指定忽略大小写模式

比如:

        System.out.println(Pattern.compile("abc", Pattern.CASE_INSENSITIVE).matcher("ABC").matches()); // true