tags:

views:

321

answers:

2

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
^.*?(?<!\.g)\.cs$
Vinko Vrsalovic
whops yes, had a small tyop .? instead of .*, ungreedy shoulnd't matter though.
thr
+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
Upvoted because this could be useful for people doing something similar in, say, JS.
eyelidlessness