I need a regex that matches all strings ending in .cs, but if they end in .g.cs they should not match. I'm using .NET regular expressions.
+7
A:
This will match the end if it's .cs but not .g.cs
(?<!\.g)\.cs$
For the entire string start to finish, something like this:
^.*(?<!\.g)\.cs$
thr
2008-10-18 18:16:17
^.*?(?<!\.g)\.cs$
Vinko Vrsalovic
2008-10-18 18:18:48
whops yes, had a small tyop .? instead of .*, ungreedy shoulnd't matter though.
thr
2008-10-18 18:22:36
+2
A:
In a regex engine without negative lookbehind:
^(.*[^g]|.*[^.]g|)\.cs$
Linguistically, general lookahead/lookbehind is non-regular, so it isn't a "regular expression" even though it's really convenient...
Irrelevant because you state that you're using .NET's regular expression engine, which supports non-regular extensions like this, but I like to make this point whenever I can.
ephemient
2008-10-18 19:55:53
Upvoted because this could be useful for people doing something similar in, say, JS.
eyelidlessness
2008-10-20 05:22:37