How to remove C-style comments from code

后端 未结 1 575
难免孤独
难免孤独 2020-12-11 11:15

I just read a new question here on SO asking basically the same thing as mine does in the title. That got me thinking - and searching the web (most hits pointed to SO, of co

相关标签:
1条回答
  • 2020-12-11 11:52

    I've considered the comments (so far) and changed the regex to:

    (?:\/\/(?:\\\n|[^\n])*\n)|(?:\/\*[\s\S]*?\*\/)|((?:R"([^(\\\s]{0,16})\([^)]*\)\2")|(?:@"[^"]*?")|(?:"(?:\?\?'|\\\\|\\"|\\\n|[^"])*?")|(?:'(?:\\\\|\\'|\\\n|[^'])*?'))
    

    It handles Biffens C++11's raw string literal (as well as C# verbatim strings) and it's changed according to Wiktors suggestions.

    Split it to handling single and double quotes separately because of difference in logic (and avoiding the non-working back reference ;).

    It's undoubtedly more complex, but still far from the solutions I've seen out there which hardly cover any of the string issues. And it could be stripped of parts not applicable to a specific language.

    One comment suggested supporting more languages. That would make the RE (even more) complex and unmanageable. It should be relatively easy to adapt though.

    Updated regex101 example.

    Thanks everyone for the input so far. And keep the suggestions coming.

    Regards

    Edit: Update Raw String - this time I actually read the spec. ;)

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