我觉得:
System.out.println(Pattern.compile("^[ab]+$",Pattern.CASE_INSENSITIVE).matcher(strRegTest).matches());太麻烦,有没有其他解决办法?

解决方案 »

  1.   

    public class MatchFlag {
        public static void main(String[] args) {
            Pattern p  = Pattern.compile("abcd(?i:EFGhijKL)mn");
            Matcher m = p.matcher("abcdefghijklmn");
            System.out.println(m.matches());
        }
    }Pattern.UNIX_LINES
     d
     Treat \n as the only line terminator.
     
    Pattern.DOTALL
     s
     Dot (.) matches any character, including a line terminator.
     
    Pattern.MULTILINE
     m
     ^ and $ match next to embedded line terminators.
     
    Pattern.COMMENTS
     x
     Ignore whitespace and allow embedded comments starting with #.
     
    Pattern.CASE_INSENSITIVE
     i
     Case-insensitive match for ASCII characters.
     
    Pattern.UNICODE_CASE
     u
     Case-insensitive match for Unicode characters.
      

  2.   

    谢谢楼上的,为什么
    String strRegTest = "aaBB";
    System.out.println(strRegTest.matches("^[(?i:ab)]+$"));
    还是这里还是打印false?
      

  3.   

    你表达式有问题
    [] 只表示一个字母, 而不是一个字串
    使用
    ^(?i:[ab]+)$
    或者
    (?i)^[ab]+$
    (?i) 代表后面的表达式一律是大小写无关的
    (?i:string) 只是冒号后面的表达式大小写无关的
      

  4.   

    当然也可以关闭
    (?-i)
    (?-i:string)
      

  5.   

    [] 确切的说是"一个字符集合中任意一个字符",
    如果不加其他修饰符的确只占"一个字符"的位置贪婪, 懒惰, 抢占(Possessive, 翻译的可能不恰当)等模式
    只在使用量化器(quantifier)时才有意义
    例如:
    a+ 是 Greedy
    a+? 是 Reluctant
    a++ 是 Possessive
      

  6.   

    我觉得对于贪婪模式这种也应该如同(?i)一样采用一个修饰符可以改变为非贪婪模式,这样会方便很多。好多语言都有这个修饰符。看来sun考虑得还不周全