Alex
Alex

Reputation: 12913

Regular expression to match line containing some strings and not others

I have lines like this:

example.com/p/stuff/...
example.com/page/thing/...
example.com/page/stuff/...
example.com/page/other-stuff/...
etc

where the dots represent continuing URL paths. I want to select URLs that contain /page/ and are NOT followed by thing/. So from the above list we would select:

example.com/page/stuff/...
example.com/page/other-stuff/...

Upvotes: 2

Views: 51

Answers (3)

maioman
maioman

Reputation: 18744

.*?\/page\/[^(thing)].*

this is the regex for matching a string which has /page/ not followed by thing

adding the lazy evalation is suggested because you advance a char at the time, better performance!

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Use the following regex pattern:

.*?\/page\/(?!thing\/).*

https://regex101.com/r/19wh1w/2


(?!thing\/) - negative lookahead assertion ensures that page/ section is not followed by thing/

Upvotes: 1

You need to use negative lookahead:

example\.com\/page\/(?!thing\/).*

Demo

Upvotes: 1

Related Questions