【发布时间】:2021-02-23 04:56:54
【问题描述】:
我想找到一个与不包含所有指定元素的字符串匹配的正则表达式,与它们的顺序无关。例如,给定以下数据:
one two three four
one three two
one two
one three
four
将单词two three 传递给正则表达式应该匹配one two、one three 和four 行。
我知道如何实现一个表达式来匹配不包含任何单词的行,只匹配行four:
^((?!two|three).)*$
但是对于我所描述的情况,我迷路了。
【问题讨论】:
-
@JvdV 它不能同时包含两个词,只要其中一个就可以了
-
另外,
^(?!.*\b(two|three)\b.*\b(?!\1)(two|three)\b).*should work。三个单词的扩展将look like this。
标签: regex regex-lookarounds regex-negation