Search code examples
regexstringsubstring

regex string does not contain substring


I am trying to match a string which does not contain a substring

My string always starts "http://www.domain.com/"

The substring I want to exclude from matches is ".a/" which comes after the string (a folder name in the domain name)

There will be characters in the string after the substring I want to exclude

For example:

"http://www.domain.com/.a/test.jpg" should not be matched

But "http://www.domain.com/test.jpg" should be


Solution

  • Use a negative lookahead assertion as:

    ^http://www\.domain\.com/(?!\.a/).*$
    

    Rubular Link

    The part (?!\.a/) fails the match if the URL is immediately followed with a .a/ string.