Regex to strip line comments from C#...
I'm working on a routine to strip block _or_ line comments from some C# code.  I have looked at the other examples on the site, but haven't found the _exact_ answer that I'm looking for.

I can match block comments (/* comment */) in their entirety using this regular expression with RegexOptions.Singleline:

`(/\*[\w\W]*\*/)`

And I can match line comments (// comment) in their entirety using this regular expression with RegexOptions.Multiline:

`(//((?!\*/).)*)(?!\*/)[^\r\n]`

_Note: I'm using `[^\r\n]` instead of `$` because `$` is including `\r` in the match, too._

However, this doesn't _quite_ work the way I want it to.

Here is my test code that I'm matching against:

    // remove whole line comments
    bool broken = false; // remove partial line comments
    if (broken == true)
    {
        return "BROKEN";
    }
    /* remove block comments
    else
    {
        return "FIXED";
    } // do not remove nested comments */ bool working = !broken;
    return "NO COMMENT";

The block expression matches 

    /* remove block comments
    else
    {
        return "FIXED";
    } // do not remove nested comments */

which is fine and good, but the line expression matches

    // remove whole line comments
    // remove partial line comments

_and_

    // do not remove nested comments

Also, if I do not have the */ positive lookahead in the line expression twice, it matches

    // do not remove nested comments *

which I _really_ don't want.

What I want is an expression that will match characters, starting with `//`, to the end of line, but does _not_ contain `*/` between the `//` and end of line.

Also, just to satisfy my curiosity, can anyone explain why I need the lookahead twice?  `(//((?!\*/).)*)[^\r\n]` and `(//(.)*)(?!\*/)[^\r\n]` will both include the *, but `(//((?!\*/).)*)(?!\*/)[^\r\n]` and `(//((?!\*/).)*(?!\*/))[^\r\n]` won't.
Regex to strip line comments from C#...
===

I'm working on a routine to strip block _or_ line comments from some C# code.  I have looked at the other examples on the site, but haven't found the _exact_ answer that I'm looking for.

I can match block comments (/* comment */) in their entirety using this regular expression with RegexOptions.Singleline:

`(/\*[\w\W]*\*/)`

And I can match line comments (// comment) in their entirety using this regular expression with RegexOptions.Multiline:

`(//((?!\*/).)*)(?!\*/)[^\r\n]`

_Note: I'm using `[^\r\n]` instead of `$` because `$` is including `\r` in the match, too._

However, this doesn't _quite_ work the way I want it to.

Here is my test code that I'm matching against:

    // remove whole line comments
    bool broken = false; // remove partial line comments
    if (broken == true)
    {
        return "BROKEN";
    }
    /* remove block comments
    else
    {
        return "FIXED";
    } // do not remove nested comments */ bool working = !broken;
    return "NO COMMENT";

The block expression matches 

    /* remove block comments
    else
    {
        return "FIXED";
    } // do not remove nested comments */

which is fine and good, but the line expression matches

    // remove whole line comments
    // remove partial line comments

_and_

    // do not remove nested comments

Also, if I do not have the */ positive lookahead in the line expression twice, it matches

    // do not remove nested comments *

which I _really_ don't want.

What I want is an expression that will match characters, starting with `//`, to the end of line, but does _not_ contain `*/` between the `//` and end of line.

Also, just to satisfy my curiosity, can anyone explain why I need the lookahead twice?  `(//((?!\*/).)*)[^\r\n]` and `(//(.)*)(?!\*/)[^\r\n]` will both include the *, but `(//((?!\*/).)*)(?!\*/)[^\r\n]` and `(//((?!\*/).)*(?!\*/))[^\r\n]` won't.