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?\"
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][^/\"'\\\\]*)"