Groovy删除多行注释

tfm*_*gue 2 regex groovy comments multiline replaceall

如何使用groovy删除多行注释?

/* Use groovy replaceAll regex to 
   remove this comment */
Run Code Online (Sandbox Code Playgroud)

我从文件,文件对象中读入上述文本,然后将其转换为字符串.如果评论跨越一行,我可以使用下面发布的replaceAll方法将其删除:

def file = new File('myfile')
def fileString = file.getText()

println fileString.replaceAll('/\\* .* \\*/','')
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用(?m)标志,但我无法识别我的模式.我尝试了以下语句,但都无法匹配我的模式.

fileString.replaceAll('(?m)/\\* (.*) \\*/'    ,'')  #multiline switch
fileString.replaceAll('(/\\*)(.|\n\r)*(\\*/)' ,'')  #match all .* (include \n\r)
Run Code Online (Sandbox Code Playgroud)

我想过使用DotAll,最后是(\ s)和$ {}.但是,我不确定如何有效地将它混合到正则表达式中.任何帮助都会受到欢迎.谢谢.

Qta*_*tax 7

试试这个表达式:

'(?s)/\\*.*?\\*/'
Run Code Online (Sandbox Code Playgroud)

(?m)没有.匹配新线,(?s)那样做.