问题: 我如何匹配一个特定的单词(这个单词是Find),同时忽略任何包含一个子字符串(这个子字符串是Exclude)的字符串?
我想做的就是找到所有包含单词Find的模式,并排除任何在字符串中包含Exclude的模式。
在模式匹配方面,我期望的是:
Find - Succeed
ExcludeFind - Fail
FineExclude - Fail
xxxFind - Succeed
Findxxx - Succeed
xxxFindxxx - Succeed
ExcludexxxxxxxxxxFind - Fail
FindxxxxxxxxxxExclude - Fail
ExcludexxxxxxxxxxFindxxxxxxxxxxExclude - Fail
我制作的正则表达式模式,根据结果的不同而变化,包括:
(\bFind\b) - Only works if Find is the only word
(\w?Find\w?) - Pattern matches any word with Find
((\w?Find\w?)([^Exclude])|([^Exclude])(\w?Find\w?)) - Image below
在查看了各种 Stack 帖子后,我尝试了更为复杂的方法,采用了 正向和反向界定 ,但它没有成功匹配任何模式。
(?<!Exclude)(Find)(?<=Exclude)
如果是
(?<!Exclude)(Find)
那么这个可以工作,但是只有两个问题 ExcludexxxxxxxxxxFind
和 FindxxxxxxxxxxExclude
也会成功匹配,这是我不想要的,因为这些字符串包含了 "Exclude" 这个单词。我认为使用通配符变量可能会起作用,例如
(?<!Exclude)?(Find)
或类似的东西,但是在这种情况下没有任何模式匹配。