Regex 使用正则表达式匹配string1,但如果string1伴随字符串2,则不匹配

Regex 使用正则表达式匹配string1,但如果string1伴随字符串2,则不匹配,regex,Regex,我只需要选择包含单词“Rack”的字符串,但是,如果字符串同时包含“Rack”和“Shelf”,我不希望选择它 blah/blah/Rack 1/Box 5/Row 2/Cell 3 blah/blah/Rack 2/Box 4/Row 2/Cell 3 blah/blah/Rack 4/Box 3/Row 2/Cell 3 blah/blah/Shelf 1/Rack 1/Box 1/Row 2/Cell 3 blah/blah/Box 3/Row 2/Cell 3 我尝试了下面这样的方法

我只需要选择包含单词“Rack”的字符串,但是,如果字符串同时包含“Rack”和“Shelf”,我不希望选择它

blah/blah/Rack 1/Box 5/Row 2/Cell 3
blah/blah/Rack 2/Box 4/Row 2/Cell 3
blah/blah/Rack 4/Box 3/Row 2/Cell 3 
blah/blah/Shelf 1/Rack 1/Box 1/Row 2/Cell 3
blah/blah/Box 3/Row 2/Cell 3
我尝试了下面这样的方法,但它仍然会选择最后一条记录

(^((?!Shelf).)*$)

您可以使用此正则表达式:

^(?!.*?\bShelf\b).*?\bRack\b.*$


(?!.\bShelf\b)
为负前瞻,如果在匹配中找到
Shelf
,则匹配失败。

我不理解为什么即使Shelf位于机架后面,也可以这样做,请您解释一下吗?这是由于前瞻
(?!.\bShelf\b)
这将否定行开始后的任何位置的匹配
Shelf
。您使用什么操作系统/数据库和/或软件在中执行正则表达式?