ChatGPT解决这个技术问题 Extra ChatGPT

Regex: match everything but a specific pattern

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).

Is there a reason why you can't match against your pattern and not do something if the string matches that?
@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.

W
Wiktor Stribiżew

Regex: match everything but:

a string starting with a specific pattern (e.g. any - empty, too - string not starting with foo): Lookahead-based solution for NFAs: ^(?!foo).*$ ^(?!foo)

Lookahead-based solution for NFAs: ^(?!foo).*$ ^(?!foo)

^(?!foo).*$

^(?!foo)

Negated character class based solution for regex engines not supporting lookarounds: ^(([^f].{2}|.[^o].|.{2}[^o]).*|.{0,2})$ ^([^f].{2}|.[^o].|.{2}[^o])|^.{0,2}$

^(([^f].{2}|.[^o].|.{2}[^o]).*|.{0,2})$

^([^f].{2}|.[^o].|.{2}[^o])|^.{0,2}$

a string ending with a specific pattern (say, no world. at the end): Lookbehind-based solution: (?

Lookbehind-based solution: (?

(?

^.*(?

Lookahead solution: ^(?!.*world\.$).* ^(?!.*world\.$)

^(?!.*world\.$).*

^(?!.*world\.$)

POSIX workaround: ^(.*([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.])|.{0,5})$ ([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.]$|^.{0,5})$

^(.*([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.])|.{0,5})$

([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.]$|^.{0,5})$

a string containing specific text (say, not match a string having foo): Lookaround-based solution: ^(?!.*foo) ^(?!.*foo).*$ POSIX workaround: Use the online regex generator at www.formauri.es/personal/pgimeno/misc/non-match-regex

Lookaround-based solution: ^(?!.*foo) ^(?!.*foo).*$

^(?!.*foo)

^(?!.*foo).*$

POSIX workaround: Use the online regex generator at www.formauri.es/personal/pgimeno/misc/non-match-regex

Use the online regex generator at www.formauri.es/personal/pgimeno/misc/non-match-regex

a string containing specific character (say, avoid matching a string having a | symbol): ^[^|]*$

^[^|]*$

a string equal to some string (say, not equal to foo): Lookaround-based: ^(?!foo$) ^(?!foo$).*$ POSIX: ^(.{0,2}|.{4,}|[^f]..|.[^o].|..[^o])$

Lookaround-based: ^(?!foo$) ^(?!foo$).*$

^(?!foo$)

^(?!foo$).*$

POSIX: ^(.{0,2}|.{4,}|[^f]..|.[^o].|..[^o])$

^(.{0,2}|.{4,}|[^f]..|.[^o].|..[^o])$

a sequence of characters: PCRE (match any text but cat): /cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/i or /cat(*SKIP)(*FAIL)|(?:(?!cat).)+/is Other engines allowing lookarounds: (cat)|[^c]*(?:c(?!at)[^c]*)* (or (?s)(cat)|(?:(?!cat).)*, or (cat)|[^c]+(?:c(?!at)[^c]*)*|(?:c(?!at)[^c]*)+[^c]*) and then check with language means: if Group 1 matched, it is not what we need, else, grab the match value if not empty

PCRE (match any text but cat): /cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/i or /cat(*SKIP)(*FAIL)|(?:(?!cat).)+/is

Other engines allowing lookarounds: (cat)|[^c]*(?:c(?!at)[^c]*)* (or (?s)(cat)|(?:(?!cat).)*, or (cat)|[^c]+(?:c(?!at)[^c]*)*|(?:c(?!at)[^c]*)+[^c]*) and then check with language means: if Group 1 matched, it is not what we need, else, grab the match value if not empty

a certain single character or a set of characters: Use a negated character class: [^a-z]+ (any char other than a lowercase ASCII letter) Matching any char(s) but |: [^|]+

Use a negated character class: [^a-z]+ (any char other than a lowercase ASCII letter)

Matching any char(s) 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\./.


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.
@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.
@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*", "").
Dear Wiktor. You have closed my question however your linked answer fails. I have updated my question stackoverflow.com/questions/60004380/…
@Dotizo Python re library is quite different from PCRE. Use PyPi regex library that supports the (*SKIP)(*FAIL) verbs.
P
Peter Mortensen

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


With grep use -P to enable lookahead.
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
This answer is wrong, a quick test shows that. I think what you meant is ^((?!foo).)*$ (stackoverflow.com/a/406408/3964381)
F
Firsh - justifiedgrid.com

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

[^=]*

will match everything but =


That's true, but it only processes one character at a time. If you want to exclude a sequence of two or more characters, you have to use negative lookahead like the other responders said.
perfect solution tu remove any undesirable character but those in the pattern. thanks
@Alan, "...you have to use a negative lookahead..." is incorrect, but we shouldn't be too hard on you because Wiktor didn't post his answer--which shows why--until 2016.
P
Peter Mortensen

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


Perhaps written str !~ /\Aindex\.php/.
P
Peter Mortensen

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>

That will reject "index_php" or "index#php".