How do I match a pattern only if there isn't a specific character before it on the same line?
I have the following regex code:
pattern = @"(?<=^|[\s.(<;])(?<!//)(" + Regex.Escape(keyword) + @")(?=[\s.(>])";
replacement = "<span style='" + keywordStyle + "'>$1</span>";
code = Regex.Replace(code, pattern, replacement);
I would like to add a criteria to only match if there aren't 2 slashes before it on the same line (C# comment).
I played around with it, and modified the pattern:
pattern = @"(?<!\/\/)(?<=^|[\s.(<;])(?<!//)(" + Regex.Escape(keyword) + @")(?=[\s.(>])";
But apparently this only works if the 2 slashes are 2 characters right before the keyword.
So this pattern wouldn't match "//foreach", but would match "// foreach".
Can negative look-behinds be used in this case, or can I accomplish this some other way, besides negative look-behinds?
Thank you.
EDIT:
Guess I wasn't clear enough. To reiterate my problem:
I'm working on syntax highlighting, and I need to find matches for c# keywords, like "foreach". However, I also need to take into account comments, which are defined by 2 slashes. I don't want to match the keyword "foreach" if it is part of a comment (2 slashes anywhere before it on the same line.
The negative lookbehind doesn't help me in this case because the slashes will not necessarily be right before the keyword, for example "// some text foreach" - I don't want this foreach to match.
So again, my question is: How can modify my pattern to only match if 2 slashes aren't anywhere before it on the same line?
Hope my question is clear now.