views:

30

answers:

1

Hi guys I am looking for a regular expression which will not match any given string that is exactly equal to a few keywords I will determine manually.

The purpose is to edit my urlrewrite.xml which can accept regexps in following format

<rule>
     <from>^/location/([A-Z]+)/name/([A-Z]+)</from>
     <to>/login?name=$2&location=$1</to>
</rule>

For example I want to redirect everything after / which is not 'login' and 'signup' to another page. I am trying following ones but none satisfies my request.

^/(?!(login|signup).*
^/(?!(login|signup)[A-Za-z0-9]+

Because I want it to match only if input is exactly 'login' or 'signup' however, it declines 'loginblabla', too.

Any solutions are highly appreciated. :)

+1  A: 

You need to add a $ anchor at the end of the lookahead:

^/(?!(login|signup)$)(.+)

Now anything that isn't exactly login or signup will be captured in group $1.

Alan Moore
+1 That would be more logical then mine, was a left over brainfart from another regex excursion yesterday.
Wrikken
It will be captured in $2. $1 will capture login or signup.
M42
Right. I forgot to add the `?:` to make that first group non-capturing.
Alan Moore
Thanks Alan. This one solved my problem: ^/(login|signup)((/.*)?)$Thanks to that one, I can forward /a/b/ schemes, too.
Ahmet Alp Balkan