First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking
in this area... it's just a whole bunch of information thrown at you and
you're left to yourself as to organizing it in your head. Typical learning
starts with basics and progresses through increasingly complex information -
I think given the inherent confusion-inducing ability of regex that kind of
documentation would be very valuable.
But anyway, I'm trying to write a regex that will parse a line of code from
a .cs file into the code and comments portions if there is a // somewhere in
the line. I realize this may need to be more complex down the road to
handle special occurrences of // other than in a comment (like in a string
literal), but I'm trying to start with the basics. So I have...
Regex regex = new Regex(@"(?<code >.+)//(?<comments>.+) ",
RegexOptions.Co mpiled);
regexMatch = regex.Match(ori ginal);
if (regexMatch.Suc cess)
{
code = regexMatch.Resu lt("${code}");
comments = regexMatch.Resu lt("${comments} ");
}
This works fine on
// A basic comment line
or
a = b; // code line with comment afterward
but on the line
/// <summary>
I end up with
code = "/"
comment = " <summary>"
I'm not understanding why the "//" in my regex seems to match the "last"
occurrence of the pattern and "skips" the match on the first two slashes of
the three. I thought by definition the first occurrence would be matched.
Indeed, if my original line is "//// something" I end up with "//" and "
something".
Who can clarify this for me? And who can point me to a *good* resource for
regex edumucation?