Regex to match a C-style multiline comment

前端 未结 7 2298
忘了有多久
忘了有多久 2020-11-22 10:04

I have a string for e.g.

String src = \"How are things today /* this is comment *\\*/ and is your code  /*\\* this is another comment */ working?\"
         


        
7条回答
  •  生来不讨喜
    2020-11-22 10:42

    System.out.println(src.replaceAll("\\/\\*.*?\\*\\/ ?", ""));
    

    You have to use the non-greedy-quantifier ? to get the regex working. I also added a ' ?' at the end of the regex to remove one space.

提交回复
热议问题