Regex: match everything but specific pattern










191















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)










share|improve this question
























  • 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















191















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)










share|improve this question
























  • 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













191












191








191


53






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)










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












7 Answers
7






active

oldest

votes


















163














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.






share|improve this answer


















  • 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


















200














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



[^=]*


will match everything but =






share|improve this answer


















  • 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


















181














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$



  • 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)$



  • 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])$



  • 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


  • 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 |: [^|]+


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






share|improve this answer























  • 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 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


















4














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






share|improve this answer























  • 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


















4














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>





share|improve this answer


















  • 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


















-1














grep -v in shell



!~ in perl



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






share|improve this answer




















  • 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



















-6














How about not using regex:



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





share|improve this answer


















  • 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









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









163














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.






share|improve this answer


















  • 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















163














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.






share|improve this answer


















  • 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













163












163








163







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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













200














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



[^=]*


will match everything but =






share|improve this answer


















  • 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















200














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



[^=]*


will match everything but =






share|improve this answer


















  • 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













200












200








200







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



[^=]*


will match everything but =






share|improve this answer













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



[^=]*


will match everything but =







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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











181














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$



  • 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)$



  • 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])$



  • 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


  • 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 |: [^|]+


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






share|improve this answer























  • 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 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















181














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$



  • 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)$



  • 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])$



  • 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


  • 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 |: [^|]+


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






share|improve this answer























  • 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 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













181












181








181







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$



  • 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)$



  • 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])$



  • 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


  • 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 |: [^|]+


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






share|improve this answer













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$



  • 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)$



  • 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])$



  • 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


  • 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 |: [^|]+


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







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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 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
















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











4














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






share|improve this answer























  • 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















4














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






share|improve this answer























  • 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













4












4








4







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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











4














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>





share|improve this answer


















  • 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















4














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>





share|improve this answer


















  • 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













4












4








4







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>





share|improve this answer













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>






share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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











-1














grep -v in shell



!~ in perl



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






share|improve this answer




















  • 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
















-1














grep -v in shell



!~ in perl



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






share|improve this answer




















  • 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














-1












-1








-1







grep -v in shell



!~ in perl



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






share|improve this answer















grep -v in shell



!~ in perl



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







share|improve this answer














share|improve this answer



share|improve this answer








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













  • 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












-6














How about not using regex:



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





share|improve this answer


















  • 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















-6














How about not using regex:



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





share|improve this answer


















  • 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













-6












-6








-6







How about not using regex:



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





share|improve this answer













How about not using regex:



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






share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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







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





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?



Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages