Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 418
Joe Smalley Avatar asked Mar 25 '11 12:03

Joe Smalley


2 Answers

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.

like image 88
codaddict Avatar answered Sep 30 '22 19:09

codaddict


My advise in such cases is not to construct overly complicated regexes whith negative lookahead assertions or such stuff.
Keep it simple and stupid!
Do 2 matches, one for the positives, and sort out later the negatives (or the other way around). Most of the time, the regexes become easier, if not trivial. And your program gets clearer.
For example, to extract all lines with foo, but not foobar, I use:

grep foo | grep -v foobar
like image 43
Ingo Avatar answered Sep 30 '22 19:09

Ingo