Regex to match a C-style multiline comment

前端 未结 7 2300
忘了有多久
忘了有多久 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:32

    Try this one:

    (//[^\n]*$|/(?!\\)\*[\s\S]*?\*(?!\\)/)
    

    If you want to exclude the parts enclused in " " then use:

    (\"[^\"]*\"(?!\\))|(//[^\n]*$|/(?!\\)\*[\s\S]*?\*(?!\\)/)
    

    the first capturing group identifies all " " parts and second capturing group gives you comments (both single line and multi line)

    copy the regular expression to regex101 if you want explanation

提交回复
热议问题