Reputation: 528
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
Reputation: 301177
You can use the following regex:
/(?=.*urgent)(?!.*not urgent)(^.*$)/
Sample match: http://regexr.com?2trsp
Upvotes: 7