388
votes

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)

7
And what specific pattern do you want to not match?Dominic Rodger
Is there a reason why you can't match against your pattern and not do something if the string matches that?Thomas Owens
@ThomasOwens: It depends. It depends on which part of the expression shall be negated. If the whole expression is to be negated, then you got a point. For example, if you want to code up "if the string doesn't contain 'Bruce' as a substring, then do something", you'd use plainly /Bruce/, and put the negation into the if statement, outside the regex. But it could be that you'd like to negate some subexpression. Say, you're looking for something like firstname lastname, where firstname is Bruce, and lastname is everything except XYZ, where XYZ is the last name of some celebrity called Bruce.mathheadinclouds

7 Answers

316
votes

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.

441
votes

Regex: match everything but:

Demo note: the newline \n is used inside negated character classes in demos to avoid match overflow to the neighboring line(s). They are not necessary when testing individual strings.

Anchor note: In many languages, use \A to define the unambiguous start of string, and \z (in Python, it is \Z, in JavaScript, $ is OK) to define the very end of the string.

Dot note: In many flavors (but not POSIX, TRE, TCL), . matches any char but a newline char. Make sure you use a corresponding DOTALL modifier (/s in PCRE/Boost/.NET/Python/Java and /m in Ruby) for the . to match any char including a newline.

Backslash note: In languages where you have to declare patterns with C strings allowing escape sequences (like \n for a newline), you need to double the backslashes escaping special characters so that the engine could treat them as literal characters (e.g. in Java, world\. will be declared as "world\\.", or use a character class: "world[.]"). Use raw string literals (Python r'\bworld\b'), C# verbatim string literals @"world\.", or slashy strings/regex literal notations like /world\./.

283
votes

You can put a ^ in the beginning of a character set to match anything but those characters.

[^=]*

will match everything but =

5
votes

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

5
votes

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>
0
votes

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

Use method Exec

    let match,
        arr = [],
        myRe = /([\s\S]+?)(?:index\.php\?id.+)/g;

    var str = 'http://regular-viragenia/index.php?id=2342343';

    while ((match = myRe.exec(str)) != null) {
         arr.push(match[1]);
    } 
    
    console.log(arr);

var myRe = /([\s\S]+?)(?:index\.php\?id=.+)/g;
var str = 'http://regular-viragenia/index.php?id=2342343';
var matches_array = myRe.exec(str);
console.log(matches_array[1]);

OR OTHER MATCH

let match,
            arr = [],
            myRe = /index.php\?id=((?:(?!index)[\s\S])*)/g;

        var str = 'http://regular-viragenia/index.php?id=2342343index.php?id=111index.php?id=222';

        while ((match = myRe.exec(str)) != null) {
             arr.push(match[1]);
        } 

        console.log(arr);
-18
votes

How about not using regex:

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