Lacer
Lacer

Reputation: 5968

regular expression - excluding results if string is present

Trying to create a regular expression that excludes results of a substring is present.

Data Set:

 http://www.cnn.com/test1
 http://www.cnn.com/test3
 http://www.cnn.com/test5
 http://www.stackflow.com/test4
 http://www.cnn.com/test3
 http://www.cnn.com/test4

exclude:

Results:

 http://www.cnn.com/test1
 http://www.cnn.com/test5
 http://www.cnn.com/test4

Upvotes: 1

Views: 41

Answers (3)

Lacer
Lacer

Reputation: 5968

Figured it out: (www.cnn.com)(?!/test3)

Upvotes: 1

Bram Vanroy
Bram Vanroy

Reputation: 28554

I'm guessing this would be fastest:

cnn\.com(?!\/test3)[a-zA-Z0-9-._~:?#@!$&'*+,;=`.\/\(\)\[\]]*

because you restrict the URL to allowed characters only.

Upvotes: 0

Patrick Haugh
Patrick Haugh

Reputation: 61063

If you want to avoid matching strings like http://www.cnn.com/test/test3 then you can use a negtive lookbehind at the end of the string

cnn\.com.*(?<!test3)$

Upvotes: 0

Related Questions