Java regex replaceAll multiline

前端 未结 3 789
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 19:02

I have a problem with the replaceAll for a multiline string:

String regex = \"\\\\s*/\\\\*.*\\\\*/\";
String testWorks = \" /** this should be replaced **/ j         


        
3条回答
  •  一生所求
    2020-12-04 20:01

    You need to use the Pattern.DOTALL flag to say that the dot should match newlines. e.g.

    Pattern.compile(regex, Pattern.DOTALL).matcher(testIllegal).replaceAll("x")
    

    or alternatively specify the flag in the pattern using (?s) e.g.

    String regex = "(?s)\\s*/\\*.*\\*/";
    

提交回复
热议问题