views:

166

answers:

2

I am currently rewriting a URL, however my RegEx expression is catching a directory I want to be ignored.

The rewrite rule is

^people/([A-Za-z0-9\-\_]+)/?$

...Which catches everything that matches. However, I would like to exclude a directory, People_Search, so for instance...

/people/John_Smith

...will forward, but

/people/People_Search

...should not suppose to be.

That's the only term I want to look for, so if it exists anywhere in the string, I want to ignore it.

Any ideas?

+1  A: 

Regex has a thing called a "non capturing negative lookahead assertion" which basically says "don't match the following". It looks like this:

^people/(?!People_Search)([A-Za-z0-9\-\_]+)/?$ 

Whether you can use this depends on the rewrite engine you use, and the level of regex support that's included in it. I'd expect that most common rewriters support this.

FYI: There are also negative lookbehind assertions(?<!), and also postive versions of the lookahead (?=) and lookbehind (?<=) assertions.

Tutorial: http://www.regular-expressions.info/lookaround.html

Cheeso
+1  A: 
^people/(?!People_Search)([A-Za-z0-9\-\_]+)/?$

A negative lookahead to prevent matching People_Search after people/

David Kanarek