Regex to match a C-style multiline comment

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

    Can't parse C/C++ style comments in Java source directly.
    Quoted strings have to be parsed at the same time and within the same regex
    because the string may embed /* or //, the start of a comment when it is just part
    of the string.

    Note there is additional regex consideration needs if raw strings constructs
    are possible in the language.

    The regex that does this feat is this.
    Where group 1 contains the Comment and group 2 contains the Non-Comment.
    For example if you were removing comments it would be:

    Find
    (/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//(?:[^\\]|\\(?:\r?\n)?)*?(?:\r?\n|$))|("[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]*)*'|[\S\s][^/"'\\]*)

    Replace
    $2


    Stringed:
    "(/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|//(?:[^\\\\]|\\\\(?:\\r?\\n)?)*?(?:\\r?\\n|$))|(\"[^\"\\\\]*(?:\\\\[\\S\\s][^\"\\\\]*)*\"|'[^'\\\\]*(?:\\\\[\\S\\s][^'\\\\]*)*'|[\\S\\s][^/\"'\\\\]*)"

    0 讨论(0)
提交回复
热议问题