Anderson Green
Anderson Green

Reputation: 31800

Specifying that a part of a regular expression should not contain another regular expression

Is there a way to specify that a regular expression should not contain a match of another regular expression?

Which regular expression (given here in Javascript-style syntax) would correspond to the regular expression /(artwork|sheep|cattle|book|literature)(s)/i, but not containing matches of /(sheep|cattle|artwork|literature)(s)/?

Upvotes: 0

Views: 107

Answers (1)

neoneye
neoneye

Reputation: 52161

if /books/ is not the answer then perhaps try with negative look ahead.. like this

=> /(?!(?:sheep|cattle|artwork|literature)(?:s))(artwork|sheep|cattle|book|literature)(s)/
>> re =~ 'books'
=> 0
>> re =~ 'bookxs'
=> nil
>> re =~ 'sheep'
=> nil
>> re =~ 'cattle'
=> nil
>> re =~ 'test'
=> nil

Upvotes: 1

Related Questions