tags:

views:

112

answers:

6

Hi to all, I need a regex able to match everything but a string starting with a specific pattern (specifically index.php and what follows, like index.php?id=2342343)

Any help appreciated. Thanks

+2  A: 

Just match /^index\.php/ then reject whatever matches it.

Kinopiko
Why won't this work?
Thomas Owens
What about pattern negation?
AJ
+2  A: 

grep -v in shell

!~ in perl

Please add more in other languages - I marked this as Community Wiki.

Arkadiy
+3  A: 

Not a regexp expert, but I think you could use a negative lookahead from the start, e.g. ^(?!foo).*$ shouldn't match anything starting with foo.

PiotrLegnica
A: 

^(?!index.php).*

David Hedlund
That will reject "index_php".
Kinopiko
There's an echo in here! :)
Bart Kiers
+1  A: 

In python:

>>> import re
>>> p='^(?!index\.php\?[0-9]+).*$'
>>> s1='index.php?12345'
>>> re.match(p,s1)
>>> s2='index.html?12345'
>>> re.match(p,s2)
<_sre.SRE_Match object at 0xb7d65fa8>
AJ
That will reject "index_php" or "index#php".
Kinopiko
Good point, forgot to escape the '.' Thanks.
AJ
+1  A: 

How about not using regex:

// In PHP
0 !== strpos($string, 'index.php')
JP