515

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pistacchio
  • 56,889
  • 107
  • 278
  • 420
  • 7
    Is there a reason why you can't match against your pattern and not do something if the string matches that? – Thomas Owens Nov 06 '09 at 13:35
  • 1
    @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 Nov 21 '19 at 12:42

6 Answers6

610

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\./.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Great write up! For the case of "a string (not) equal to some string", with the example of `^(?!foo$)`, why is it that the dollar sign has to be within the parentheses for the expression to work? I was expecting `^(?!foo)$` to give the same results, but it does not. – Grant Humphries Jan 07 '17 at 17:10
  • 6
    @GrantHumphries: When the `$` anchor is inside the lookahead, it is part of the condition, part of that *zero-width assertion*. If it were outside, like in `^(?!foo)$`, it will be part of the *consuming* pattern requiring the end of string right after the start of string, making the negative lookahead irrelevant since it would always return *true* (there cannot be any text after the end of string, let alone `foo`). So, `^(?!foo$)` matches start of a string that is not followed with `foo` that is followed with the string end. `^(?!foo)$` matches an empty string. – Wiktor Stribiżew Jan 07 '17 at 19:12
  • @robots.txt Please remove these comments. You are asking an XY question. Character classes are meant to match single chars, there is no way to define a sequence of chars with them. You should probably just find the substring between the start of a string and the first occurrence of `cot` or `lan`, and remove the match, like [`regex.replace(myString, "^.*?(?:cot|lan)\s*", "")`](https://regex101.com/r/23R4Lu/2). – Wiktor Stribiżew Jul 06 '19 at 20:48
  • Dear Wiktor. You have closed my question however your linked answer fails. I have updated my question https://stackoverflow.com/questions/60004380/how-to-remove-any-given-string-pairs-from-text – Furkan Gözükara Jan 31 '20 at 13:09
  • For example your linked answer fails at this example "ing packages editors now use--> Lorem Ipsum" – Furkan Gözükara Jan 31 '20 at 13:09
  • why (*SKIP)(*FAIL) raise an error in python? how to solve this? – TheFaultInOurStars Dec 10 '20 at 22:13
  • 1
    @Dotizo Python `re` library is quite different from PCRE. Use [PyPi regex library](https://pypi.org/project/regex/) that supports the `(*SKIP)(*FAIL)` verbs. – Wiktor Stribiżew Dec 10 '20 at 22:22
  • 1
    Given the title of the question, this is much more useful than the accepted answer. The links to regex101 also makes it much easier to test and understand. All in all an awesome answer which I'll undoubtedly use as a reference in the future! – Snailedlt Aug 03 '23 at 09:15
361

You could use a negative lookahead from the start, e.g., ^(?!foo).*$ shouldn't match anything starting with foo.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
335

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

[^=]*

will match everything but =

3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

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>
InSync
  • 4,851
  • 4
  • 8
  • 30
AJ.
  • 27,586
  • 18
  • 84
  • 94
-1

Came across this thread after a long search. I had this problem for multiple searches and replace of some occurrences. But the pattern I used was matching till the end. Example below

import re

text = "start![image]xxx(xx.png) yyy xx![image]xxx(xxx.png) end"
replaced_text = re.sub(r'!\[image\](.*)\(.*\.png\)', '*', text)
print(replaced_text)

gave

start* end

Basically, the regex was matching from the first ![image] to the last .png, swallowing the middle yyy

Used the method posted above https://stackoverflow.com/a/17761124/429476 by Firish to break the match between the occurrence. Here the space is not matched; as the words are separated by space.

replaced_text = re.sub(r'!\[image\]([^ ]*)\([^ ]*\.png\)', '*', text)

and got what I wanted

start* yyy xx* end
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71