大家帮看看这个正则表达式哪里出问题了呢?
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就不匹配了。
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就不匹配了。
替换为如下代码:
String reg = "(?s)\\[color=\\w+\\].*?\\[/color\\]";
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.