[英]Regex: Do not match string with words preceding target word
如何找到目标词的正则表达式匹配项,但是如果该目标词前面带有否定词(例如“ not”),则不会给出匹配项。
否定词必须在目标词之前的一定范围内,因此对于另一个否定它不会向后看
例:
Target = 'word'
+------------------------------------------+-------------+
| 'bananas is a word' | match |
| '76 is not a word' | NOT a match |
| '76 is not a word but bananas is a word' | match |
+------------------------------------------+-------------+
注意:
'76 is not a word but bananas is a word'
包含不能不 匹配 。 这就是为什么我不能使用^$
。
理想情况下,我可以包含多个否定词,因此正则表达式如下所示:
.{1,25}(?<!(isn't|not).{1,10}) word
如果该模式始终遵循以下内容:
bananas is a word
76 is not a word
76 is not a word but bananas is a word
bananas is a word but 76 is not a word
bananas is a word and bird is a word
76 is not a word and 86 isn't a word either
那么您可以使用以下正则表达式:
([a-zA-Z0-9]*)\sis(?!'nt|\snot)\sa\sword
请注意,同时使用捕获组和非捕获组,以确保isn't
或is not
被正确排除在匹配之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.