cancelledout
cancelledout

Reputation: 528

Regex to exclude a substring that exists before a certain substring

Any idea on how to write a regular expression that matches a substring 'urgent' as long as it doesn't contain 'not' before 'urgent'?

ex.

this is urgent - match!
[not urgent] blah - don't match

For Ruby 1.9.2, this works /(?<!not )urgent/ -uses negative look behind

Upvotes: 2

Views: 2927

Answers (1)

manojlds
manojlds

Reputation: 301177

You can use the following regex:

/(?=.*urgent)(?!.*not urgent)(^.*$)/

Sample match: http://regexr.com?2trsp

Upvotes: 7

Related Questions