Regex: match everything but specific pattern
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)
regex
add a comment |
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)
regex
And what specific pattern do you want to not match?
– Dominic Rodger
Nov 6 '09 at 13:35
1
Is there a reason why you can't match against your pattern and not do something if the string matches that?
– Thomas Owens
Nov 6 '09 at 13:35
2
Possible duplicate of Regular expression to match a line that doesn't contain a word?
– 7vujy0f0hy
Nov 9 '17 at 9:22
add a comment |
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)
regex
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)
regex
regex
edited Jan 17 at 11:24
SirPeople
2,114823
2,114823
asked Nov 6 '09 at 13:34
pistacchiopistacchio
21.5k81230354
21.5k81230354
And what specific pattern do you want to not match?
– Dominic Rodger
Nov 6 '09 at 13:35
1
Is there a reason why you can't match against your pattern and not do something if the string matches that?
– Thomas Owens
Nov 6 '09 at 13:35
2
Possible duplicate of Regular expression to match a line that doesn't contain a word?
– 7vujy0f0hy
Nov 9 '17 at 9:22
add a comment |
And what specific pattern do you want to not match?
– Dominic Rodger
Nov 6 '09 at 13:35
1
Is there a reason why you can't match against your pattern and not do something if the string matches that?
– Thomas Owens
Nov 6 '09 at 13:35
2
Possible duplicate of Regular expression to match a line that doesn't contain a word?
– 7vujy0f0hy
Nov 9 '17 at 9:22
And what specific pattern do you want to not match?
– Dominic Rodger
Nov 6 '09 at 13:35
And what specific pattern do you want to not match?
– Dominic Rodger
Nov 6 '09 at 13:35
1
1
Is there a reason why you can't match against your pattern and not do something if the string matches that?
– Thomas Owens
Nov 6 '09 at 13:35
Is there a reason why you can't match against your pattern and not do something if the string matches that?
– Thomas Owens
Nov 6 '09 at 13:35
2
2
Possible duplicate of Regular expression to match a line that doesn't contain a word?
– 7vujy0f0hy
Nov 9 '17 at 9:22
Possible duplicate of Regular expression to match a line that doesn't contain a word?
– 7vujy0f0hy
Nov 9 '17 at 9:22
add a comment |
7 Answers
7
active
oldest
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.
3
With grep use -P to enable lookahead.
– Seppo Enarvi
May 31 '16 at 10:28
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
– dave_k_smith
Aug 11 '16 at 21:02
6
This answer is wrong, a quick test shows that. I think what you meant is^((?!foo).)*$(stackoverflow.com/a/406408/3964381)
– gilad mayani
Jun 22 '17 at 12:28
add a comment |
You can put a ^ in the beginning of a character set to match anything but those characters.
[^=]*
will match everything but =
36
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.
– Alan Moore
Jul 20 '13 at 10:42
add a comment |
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)
- 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$
- Lookahead-based solution for NFAs:
- a string ending with a specific pattern (say, no
world.at the end):- Lookbehind-based solution:
(?<!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)$
- Lookbehind-based solution:
- a string containing specific text (say, not match a string having
foo) (no POSIX compliant patern, sorry):^(?!.*foo)^(?!.*foo).*$
- 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:
- a sequence of characters:
PCRE (match any text butcat):/cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/ior/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:
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'bworldb'), 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.
– Grant Humphries
Jan 7 '17 at 17:10
3
@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 alonefoo). So,^(?!foo$)matches start of a string that is not followed withfoothat is followed with the string end.^(?!foo)$matches an empty string.
– Wiktor Stribiżew
Jan 7 '17 at 19:12
add a comment |
Just match /^index.php/ then reject whatever matches it.
Why won't this work?
– Thomas Owens
Nov 6 '09 at 13:38
1
What about pattern negation?
– AJ.
Nov 6 '09 at 13:38
add a comment |
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>
3
That will reject "index_php" or "index#php".
– user181548
Nov 6 '09 at 13:43
1
Good point, forgot to escape the '.' Thanks.
– AJ.
Nov 6 '09 at 13:44
add a comment |
grep -v in shell
!~ in perl
Please add more in other languages - I marked this as Community Wiki.
10
The OP specifically requested a regex... I'm not sure this helps! (He certainly has his reasons for requesting a regex; he didn't ask, "How can I solve this with any arbitrary technology?")
– rinogo
Nov 19 '15 at 21:56
add a comment |
How about not using regex:
// In PHP
0 !== strpos($string, 'index.php')
6
The OP specifically requested a regex... I'm not sure this helps! (He may be usinggrepon the command-line, for example, or Perl/Python/any other language, or an "Execute this regex for every line" command in a text editor, etc...)
– rinogo
Nov 19 '15 at 21:56
add a comment |
protected by Wiktor Stribiżew Sep 8 '16 at 7:00
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
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.
3
With grep use -P to enable lookahead.
– Seppo Enarvi
May 31 '16 at 10:28
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
– dave_k_smith
Aug 11 '16 at 21:02
6
This answer is wrong, a quick test shows that. I think what you meant is^((?!foo).)*$(stackoverflow.com/a/406408/3964381)
– gilad mayani
Jun 22 '17 at 12:28
add a comment |
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.
3
With grep use -P to enable lookahead.
– Seppo Enarvi
May 31 '16 at 10:28
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
– dave_k_smith
Aug 11 '16 at 21:02
6
This answer is wrong, a quick test shows that. I think what you meant is^((?!foo).)*$(stackoverflow.com/a/406408/3964381)
– gilad mayani
Jun 22 '17 at 12:28
add a comment |
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.
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.
answered Nov 6 '09 at 13:40
Cat Plus PlusCat Plus Plus
90.8k23167202
90.8k23167202
3
With grep use -P to enable lookahead.
– Seppo Enarvi
May 31 '16 at 10:28
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
– dave_k_smith
Aug 11 '16 at 21:02
6
This answer is wrong, a quick test shows that. I think what you meant is^((?!foo).)*$(stackoverflow.com/a/406408/3964381)
– gilad mayani
Jun 22 '17 at 12:28
add a comment |
3
With grep use -P to enable lookahead.
– Seppo Enarvi
May 31 '16 at 10:28
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
– dave_k_smith
Aug 11 '16 at 21:02
6
This answer is wrong, a quick test shows that. I think what you meant is^((?!foo).)*$(stackoverflow.com/a/406408/3964381)
– gilad mayani
Jun 22 '17 at 12:28
3
3
With grep use -P to enable lookahead.
– Seppo Enarvi
May 31 '16 at 10:28
With grep use -P to enable lookahead.
– Seppo Enarvi
May 31 '16 at 10:28
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
– dave_k_smith
Aug 11 '16 at 21:02
If not matching "foo" or "bar" is your desired behavior, check this answer: stackoverflow.com/a/2404330/874824
– dave_k_smith
Aug 11 '16 at 21:02
6
6
This answer is wrong, a quick test shows that. I think what you meant is
^((?!foo).)*$ (stackoverflow.com/a/406408/3964381)– gilad mayani
Jun 22 '17 at 12:28
This answer is wrong, a quick test shows that. I think what you meant is
^((?!foo).)*$ (stackoverflow.com/a/406408/3964381)– gilad mayani
Jun 22 '17 at 12:28
add a comment |
You can put a ^ in the beginning of a character set to match anything but those characters.
[^=]*
will match everything but =
36
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.
– Alan Moore
Jul 20 '13 at 10:42
add a comment |
You can put a ^ in the beginning of a character set to match anything but those characters.
[^=]*
will match everything but =
36
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.
– Alan Moore
Jul 20 '13 at 10:42
add a comment |
You can put a ^ in the beginning of a character set to match anything but those characters.
[^=]*
will match everything but =
You can put a ^ in the beginning of a character set to match anything but those characters.
[^=]*
will match everything but =
answered Jul 20 '13 at 10:13
Firsh - LetsWP.ioFirsh - LetsWP.io
2,66721624
2,66721624
36
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.
– Alan Moore
Jul 20 '13 at 10:42
add a comment |
36
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.
– Alan Moore
Jul 20 '13 at 10:42
36
36
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.
– Alan Moore
Jul 20 '13 at 10:42
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.
– Alan Moore
Jul 20 '13 at 10:42
add a comment |
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)
- 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$
- Lookahead-based solution for NFAs:
- a string ending with a specific pattern (say, no
world.at the end):- Lookbehind-based solution:
(?<!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)$
- Lookbehind-based solution:
- a string containing specific text (say, not match a string having
foo) (no POSIX compliant patern, sorry):^(?!.*foo)^(?!.*foo).*$
- 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:
- a sequence of characters:
PCRE (match any text butcat):/cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/ior/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:
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'bworldb'), 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.
– Grant Humphries
Jan 7 '17 at 17:10
3
@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 alonefoo). So,^(?!foo$)matches start of a string that is not followed withfoothat is followed with the string end.^(?!foo)$matches an empty string.
– Wiktor Stribiżew
Jan 7 '17 at 19:12
add a comment |
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)
- 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$
- Lookahead-based solution for NFAs:
- a string ending with a specific pattern (say, no
world.at the end):- Lookbehind-based solution:
(?<!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)$
- Lookbehind-based solution:
- a string containing specific text (say, not match a string having
foo) (no POSIX compliant patern, sorry):^(?!.*foo)^(?!.*foo).*$
- 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:
- a sequence of characters:
PCRE (match any text butcat):/cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/ior/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:
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'bworldb'), 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.
– Grant Humphries
Jan 7 '17 at 17:10
3
@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 alonefoo). So,^(?!foo$)matches start of a string that is not followed withfoothat is followed with the string end.^(?!foo)$matches an empty string.
– Wiktor Stribiżew
Jan 7 '17 at 19:12
add a comment |
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)
- 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$
- Lookahead-based solution for NFAs:
- a string ending with a specific pattern (say, no
world.at the end):- Lookbehind-based solution:
(?<!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)$
- Lookbehind-based solution:
- a string containing specific text (say, not match a string having
foo) (no POSIX compliant patern, sorry):^(?!.*foo)^(?!.*foo).*$
- 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:
- a sequence of characters:
PCRE (match any text butcat):/cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/ior/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:
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'bworldb'), C# verbatim string literals @"world.", or slashy strings/regex literal notations like /world./.
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)
- 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$
- Lookahead-based solution for NFAs:
- a string ending with a specific pattern (say, no
world.at the end):- Lookbehind-based solution:
(?<!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)$
- Lookbehind-based solution:
- a string containing specific text (say, not match a string having
foo) (no POSIX compliant patern, sorry):^(?!.*foo)^(?!.*foo).*$
- 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:
- a sequence of characters:
PCRE (match any text butcat):/cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/ior/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:
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'bworldb'), C# verbatim string literals @"world.", or slashy strings/regex literal notations like /world./.
answered Jun 23 '16 at 10:12
Wiktor StribiżewWiktor Stribiżew
313k16133210
313k16133210
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 7 '17 at 17:10
3
@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 alonefoo). So,^(?!foo$)matches start of a string that is not followed withfoothat is followed with the string end.^(?!foo)$matches an empty string.
– Wiktor Stribiżew
Jan 7 '17 at 19:12
add a comment |
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 7 '17 at 17:10
3
@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 alonefoo). So,^(?!foo$)matches start of a string that is not followed withfoothat is followed with the string end.^(?!foo)$matches an empty string.
– Wiktor Stribiżew
Jan 7 '17 at 19:12
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 7 '17 at 17:10
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 7 '17 at 17:10
3
3
@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 7 '17 at 19:12
@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 7 '17 at 19:12
add a comment |
Just match /^index.php/ then reject whatever matches it.
Why won't this work?
– Thomas Owens
Nov 6 '09 at 13:38
1
What about pattern negation?
– AJ.
Nov 6 '09 at 13:38
add a comment |
Just match /^index.php/ then reject whatever matches it.
Why won't this work?
– Thomas Owens
Nov 6 '09 at 13:38
1
What about pattern negation?
– AJ.
Nov 6 '09 at 13:38
add a comment |
Just match /^index.php/ then reject whatever matches it.
Just match /^index.php/ then reject whatever matches it.
answered Nov 6 '09 at 13:36
user181548
Why won't this work?
– Thomas Owens
Nov 6 '09 at 13:38
1
What about pattern negation?
– AJ.
Nov 6 '09 at 13:38
add a comment |
Why won't this work?
– Thomas Owens
Nov 6 '09 at 13:38
1
What about pattern negation?
– AJ.
Nov 6 '09 at 13:38
Why won't this work?
– Thomas Owens
Nov 6 '09 at 13:38
Why won't this work?
– Thomas Owens
Nov 6 '09 at 13:38
1
1
What about pattern negation?
– AJ.
Nov 6 '09 at 13:38
What about pattern negation?
– AJ.
Nov 6 '09 at 13:38
add a comment |
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>
3
That will reject "index_php" or "index#php".
– user181548
Nov 6 '09 at 13:43
1
Good point, forgot to escape the '.' Thanks.
– AJ.
Nov 6 '09 at 13:44
add a comment |
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>
3
That will reject "index_php" or "index#php".
– user181548
Nov 6 '09 at 13:43
1
Good point, forgot to escape the '.' Thanks.
– AJ.
Nov 6 '09 at 13:44
add a comment |
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>
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>
answered Nov 6 '09 at 13:41
AJ.AJ.
20.5k156983
20.5k156983
3
That will reject "index_php" or "index#php".
– user181548
Nov 6 '09 at 13:43
1
Good point, forgot to escape the '.' Thanks.
– AJ.
Nov 6 '09 at 13:44
add a comment |
3
That will reject "index_php" or "index#php".
– user181548
Nov 6 '09 at 13:43
1
Good point, forgot to escape the '.' Thanks.
– AJ.
Nov 6 '09 at 13:44
3
3
That will reject "index_php" or "index#php".
– user181548
Nov 6 '09 at 13:43
That will reject "index_php" or "index#php".
– user181548
Nov 6 '09 at 13:43
1
1
Good point, forgot to escape the '.' Thanks.
– AJ.
Nov 6 '09 at 13:44
Good point, forgot to escape the '.' Thanks.
– AJ.
Nov 6 '09 at 13:44
add a comment |
grep -v in shell
!~ in perl
Please add more in other languages - I marked this as Community Wiki.
10
The OP specifically requested a regex... I'm not sure this helps! (He certainly has his reasons for requesting a regex; he didn't ask, "How can I solve this with any arbitrary technology?")
– rinogo
Nov 19 '15 at 21:56
add a comment |
grep -v in shell
!~ in perl
Please add more in other languages - I marked this as Community Wiki.
10
The OP specifically requested a regex... I'm not sure this helps! (He certainly has his reasons for requesting a regex; he didn't ask, "How can I solve this with any arbitrary technology?")
– rinogo
Nov 19 '15 at 21:56
add a comment |
grep -v in shell
!~ in perl
Please add more in other languages - I marked this as Community Wiki.
grep -v in shell
!~ in perl
Please add more in other languages - I marked this as Community Wiki.
answered Nov 6 '09 at 13:38
community wiki
Arkadiy
10
The OP specifically requested a regex... I'm not sure this helps! (He certainly has his reasons for requesting a regex; he didn't ask, "How can I solve this with any arbitrary technology?")
– rinogo
Nov 19 '15 at 21:56
add a comment |
10
The OP specifically requested a regex... I'm not sure this helps! (He certainly has his reasons for requesting a regex; he didn't ask, "How can I solve this with any arbitrary technology?")
– rinogo
Nov 19 '15 at 21:56
10
10
The OP specifically requested a regex... I'm not sure this helps! (He certainly has his reasons for requesting a regex; he didn't ask, "How can I solve this with any arbitrary technology?")
– rinogo
Nov 19 '15 at 21:56
The OP specifically requested a regex... I'm not sure this helps! (He certainly has his reasons for requesting a regex; he didn't ask, "How can I solve this with any arbitrary technology?")
– rinogo
Nov 19 '15 at 21:56
add a comment |
How about not using regex:
// In PHP
0 !== strpos($string, 'index.php')
6
The OP specifically requested a regex... I'm not sure this helps! (He may be usinggrepon the command-line, for example, or Perl/Python/any other language, or an "Execute this regex for every line" command in a text editor, etc...)
– rinogo
Nov 19 '15 at 21:56
add a comment |
How about not using regex:
// In PHP
0 !== strpos($string, 'index.php')
6
The OP specifically requested a regex... I'm not sure this helps! (He may be usinggrepon the command-line, for example, or Perl/Python/any other language, or an "Execute this regex for every line" command in a text editor, etc...)
– rinogo
Nov 19 '15 at 21:56
add a comment |
How about not using regex:
// In PHP
0 !== strpos($string, 'index.php')
How about not using regex:
// In PHP
0 !== strpos($string, 'index.php')
answered Nov 6 '09 at 13:50
JP.JP.
95068
95068
6
The OP specifically requested a regex... I'm not sure this helps! (He may be usinggrepon the command-line, for example, or Perl/Python/any other language, or an "Execute this regex for every line" command in a text editor, etc...)
– rinogo
Nov 19 '15 at 21:56
add a comment |
6
The OP specifically requested a regex... I'm not sure this helps! (He may be usinggrepon the command-line, for example, or Perl/Python/any other language, or an "Execute this regex for every line" command in a text editor, etc...)
– rinogo
Nov 19 '15 at 21:56
6
6
The OP specifically requested a regex... I'm not sure this helps! (He may be using
grep on the command-line, for example, or Perl/Python/any other language, or an "Execute this regex for every line" command in a text editor, etc...)– rinogo
Nov 19 '15 at 21:56
The OP specifically requested a regex... I'm not sure this helps! (He may be using
grep on the command-line, for example, or Perl/Python/any other language, or an "Execute this regex for every line" command in a text editor, etc...)– rinogo
Nov 19 '15 at 21:56
add a comment |
protected by Wiktor Stribiżew Sep 8 '16 at 7:00
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
And what specific pattern do you want to not match?
– Dominic Rodger
Nov 6 '09 at 13:35
1
Is there a reason why you can't match against your pattern and not do something if the string matches that?
– Thomas Owens
Nov 6 '09 at 13:35
2
Possible duplicate of Regular expression to match a line that doesn't contain a word?
– 7vujy0f0hy
Nov 9 '17 at 9:22