Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

How to match everything that does not contain foobar

Last post 10-05-2008, 1:13 PM by prometheuzz. 3 replies.
Sort Posts: Previous Next
  •  10-05-2008, 10:45 AM 46891

    How to match everything that does not contain foobar

    As I'm using PCRE in PHP every now and then I find it pretty easy to come up with a pattern when I now WHAT to match. But the other way round... when it comes to patterns or pattern parts that need to describe what NOT to match... this really gives me headaches sometimes...

    So starting with a basic thing: what is the simplest pcre pattern that matches any whole string but only if that string does not contain "foobar"?

  •  10-05-2008, 10:59 AM 46893 in reply to 46891

    Re: How to match everything that does not contain foobar

    This regex will do just that:

    ^((?!foobar).)*$

  •  10-05-2008, 12:03 PM 46894 in reply to 46893

    Re: How to match everything that does not contain foobar

    OK, using assertions, thank you!

    So i guess this is equivalent:

    ^(.(?<!foobar))*$

  •  10-05-2008, 1:13 PM 46895 in reply to 46894

    Re: How to match everything that does not contain foobar

    schnizZzla:

    OK, using assertions, thank you!

    So i guess this is equivalent:

    ^(.(?<!foobar))*$

    Correct.

    And you're welcome!

View as RSS news feed in XML