Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex-如果子字符串出现在字符串中的任何位置,则排除模式_Regex - Fatal编程技术网

Regex-如果子字符串出现在字符串中的任何位置,则排除模式

Regex-如果子字符串出现在字符串中的任何位置,则排除模式,regex,Regex,问题:如何在忽略任何包含子字符串(单词Exclude)的字符串的同时,对特定单词(正在查找的单词)进行模式匹配 我要做的是查找包含单词find的所有模式,并排除字符串中任何包含exclude的模式 我对模式匹配的期望是: Find - Succeed ExcludeFind - Fail FineExclude - Fail xxxFind - Succeed Findxxx - Succeed xxxFindxxx - Succeed ExcludexxxxxxxxxxFind - Fail

问题:如何在忽略任何包含子字符串(单词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

通过查看各种堆栈帖子,我尝试了一种通过正面和负面lookbehind的更复杂的方法,但在任何模式上都没有成功

(?<!Exclude)(Find)(?<=Exclude)
(?
如果是
(?那么这是可行的,但是只有两个问题
excludexxxxxxxxxfind
findxxxxxxxxexclude
也成功了,这是我不想要的,因为这些字符串包含“Exclude”字

我认为通配符变体可以工作
(?或类似的东西,但同样,没有模式与这种情况相匹配。

这似乎有效:

^(?!.*Exclude).*Find
它基本上是对
Exclude
的一个负的先行查找,然后查找
Find

它匹配4行:1、4、5和6。它与“Find”后面的内容不匹配,但这可能不会影响您。如果需要它匹配整个字符串,请改用它,这样它也会匹配后面的任何内容:

^(?!.*Exclude).*Find.*$
如果出现以下其他问题,请在帮助下完成:


我从公认答案中的正则表达式开始,只是替换字符串并删除斜杠,但我认为它仍然不必要地复杂,所以我开始删除东西,这是我能想到的最小工作版本。

基本问题:它必须是正则表达式吗?如果你使用的是编程language,使用不同的方法可能会容易得多。顺便说一句,检查你的第三条测试线是否说
Fine
,而不是
Find
。现在刚刚看到了这一点,但感谢Andrew-工作得很好。@CarySwoveland,他似乎想匹配
Findxxx
,所以我没有包括任何边界。