【发布时间】:2011-05-08 10:37:07
【问题描述】:
我对多行字符串的 replaceAll 有疑问:
String regex = "\\s*/\\*.*\\*/";
String testWorks = " /** this should be replaced **/ just text";
String testIllegal = " /** this should be replaced \n **/ just text";
testWorks.replaceAll(regex, "x");
testIllegal.replaceAll(regex, "x");
以上适用于 testWorks,但不适用于 testIllegal!? 为什么会这样,我该如何克服?我需要替换像注释 /* ... */ 这样跨越多行的内容。
【问题讨论】:
-
那么这个字符串呢:
"String s = \"/*\"; /* comment */" -
嗯,重点是数学正则表达式应该只匹配字符串的开头。现在看起来像这样:(?s)^\\s*/\*.*\*/ 不确定,如果让它不情愿 (?s)^\\s*/\*.*?\*/
标签: java regex replaceall