有没有办法指定正则表达式不应包含另一个正则表达式的匹配?
哪个正则表达式(在此处以Javascript样式语法给出)将对应于正则表达式/(artwork|sheep|cattle|book|literature)(s)/i
,但不包含/(sheep|cattle|artwork|literature)(s)/
的匹配项?
答案 0 :(得分:1)
如果/books/
不是答案,那么也许可以尝试使用负面预测..就像这样
=> /(?!(?: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