大家帮看看这个正则表达式哪里出问题了呢?
String s = "1. blue\n  blue   2. red red ";String reg = "\\[color=\\w+\\].*?\\[/color\\]";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(s);
while(m.find()) {
  System.out.println(m.group());
}
只输出了“red red 
blue\n  blue  ” 这里有反斜杠\n就不匹配了。

解决方案 »

  1.   

    默认情况下.不能代表换行符。可以通过打开单行模式的开关,使其可以代表换行
    替换为如下代码:
    String reg = "(?s)\\[color=\\w+\\].*?\\[/color\\]";
      

  2.   


    Pattern p = Pattern.compile(reg,Pattern.DOTALL);In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.