You can only detect comments in a robust way if you do properly tokenize into
- tokens that can contain comment start/end character sequences (all flawours of strings, characters)
- tokens that cannot contain comment start/end character sequences (all but the above tokens and but comments)
- line comments
- block comments
You may have a look at
Escaping in C#: characters, strings, string formats, keywords, identifiers[
^] at the bottom, see the bonus code for tokenizing C# with Regex, including line and block comments, taking in care of the above mentioned tokens.
Cheers
Andi