我需要逻辑和在正则表达式。
类似的
杰克和詹姆斯
同意以下字符串
'嗨,杰克,我是詹姆斯' '嗨,詹姆斯,我是杰克'
我需要逻辑和在正则表达式。
类似的
杰克和詹姆斯
同意以下字符串
'嗨,杰克,我是詹姆斯' '嗨,詹姆斯,我是杰克'
您可以使用正的正面进行检查。下面是不可或缺的regular-expressions.info的摘要:
向前看和往后看,合称为“回头看” 长度为零的断言…Lookaround实际上是匹配字符的,但是 然后放弃匹配,只返回结果:匹配或不匹配。 这就是为什么它们被称为“断言”。他们不消费 字符串中的字符,但仅断言是否可能匹配 与否。
然后,它继续解释正查找头用于断言后面的内容匹配某个表达式,而不占用该匹配表达式中的字符。
因此,下面是一个表达式,它使用两个后续的正lookhead来断言短语以任意顺序匹配jack和james:
^(?=.*\bjack\b)(?=.*\bjames\b).*$
测试它。
以?=开头的括号中的表达式是正的表头。我将分解这个模式:
^声明要匹配的表达式的开始。 (?=.*\bjack\b)是第一个正向前视,表示后面的内容必须匹配。*\bjack\b。 .*表示任意字符0次或多次。 \b表示任何单词边界(空白、表达式开始、表达式结束等)。 杰克就是连续出现的这四个角色(下一个正面展望中的詹姆斯也是如此)。 $断言表达式的结尾。
So the first lookahead says "what follows (and is not itself a lookahead or lookbehind) must be an expression that starts with zero or more of any characters followed by a word boundary and then jack and another word boundary," and the second look ahead says "what follows must be an expression that starts with zero or more of any characters followed by a word boundary and then james and another word boundary." After the two lookaheads is .* which simply matches any characters zero or more times and $ which matches the end of the expression.
"start with anything then jack or james then end with anything" satisfies the first lookahead because there are a number of characters then the word jack, and it satisfies the second lookahead because there are a number of characters (which just so happens to include jack, but that is not necessary to satisfy the second lookahead) then the word james. Neither lookahead asserts the end of the expression, so the .* that follows can go beyond what satisfies the lookaheads, such as "then end with anything".
I think you get the idea, but just to be absolutely clear, here is with jack and james reversed, i.e. "start with anything then james or jack then end with anything"; it satisfies the first lookahead because there are a number of characters then the word james, and it satisfies the second lookahead because there are a number of characters (which just so happens to include james, but that is not necessary to satisfy the second lookahead) then the word jack. As before, neither lookahead asserts the end of the expression, so the .* that follows can go beyond what satisfies the lookaheads, such as "then end with anything".
这种方法的优点是可以轻松指定多个条件。
^(?=.*\bjack\b)(?=.*\bjames\b)(?=.*\bjason\b)(?=.*\bjules\b).*$
我要写的命令说明:-
. 表示任何字符、数字都可以代替。
*表示零次或多次在它前面写的东西。
|表示“或”。
So,
james.*jack
会搜索詹姆斯,然后任意数量的角色,直到杰克出现。
既然你想要任何一个杰克。詹姆斯还是詹姆斯,杰克
于是命令:
jack.*james|james.*jack
它又短又甜
(?=.*jack)(?=.*james)
测试用例:
[ “xxx James xxx jack xxx”, “jack XXX James”, “jack XXX jam”, “果酱和杰克”, “杰克”, “詹姆斯”, ] .forEach (s = > console.log(/(? =。*詹姆斯)(? =。*杰克)/ test (s)))
Vim有一个分支操作符\&,在以任何顺序搜索包含一组单词的行时非常有用。此外,扩展所需的单词集是微不足道的。
例如,
/ *杰克\ &。*詹姆斯
将以任意顺序匹配包含jack和james的行。
有关用法的更多信息,请参阅这个答案。我不知道有任何其他实现分支的正则表达式;这个运算符甚至没有在正则表达式维基百科条目中被记录。
这个答案中的表达式对一个杰克和一个詹姆斯的任何顺序都是如此。
在这里,我们将探索其他场景。
方法一:一个杰克,一个詹姆斯
以防万一,两个jack或两个james是不允许的,只有一个jack和一个james是有效的,我们可以设计一个类似这样的表达式:
^(?!.*\bjack\b.*\bjack\b)(?!.*\bjames\b.*\bjames\b)(?=.*\bjames\b)(?=.*\bjack\b).*$
在这里,我们将使用以下语句排除这些实例:
(?!.*\bjack\b.*\bjack\b)
and,
(?!.*\bjames\b.*\bjames\b)
RegEx演示1
我们还可以将其简化为:
^(?!.*\bjack\b.*\bjack\b|.*\bjames\b.*\bjames\b)(?=.*\bjames\b|.*\bjack\b).*$
RegEx演示2
如果您希望简化/更新/探索表达式,regex101.com的右上方面板中有解释。如果您感兴趣,可以在这个调试器链接中查看匹配的步骤或修改它们。调试器演示了RegEx引擎如何一步一步地使用一些示例输入字符串并执行匹配过程。
RegEx电路
jex。Im可视化正则表达式:
Test
const regex = /^(?!.*\bjack\b.*\bjack\b|.*\bjames\b.*\bjames\b)(?=.*\bjames\b|.*\bjack\b).*$/gm; const str = `hi jack here is james hi james here is jack hi james jack here is jack james hi jack james here is james jack hi jack jack here is jack james hi james james here is james jack hi jack jack jack here is james `; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }
方法二:一个杰克,一个詹姆斯,按特定顺序
这个表达式也可以设计为先a james后a jack,类似于下面这个:
^(?!.*\bjack\b.*\bjack\b|.*\bjames\b.*\bjames\b)(?=.*\bjames\b.*\bjack\b).*$
RegEx演示3
反之亦然:
^(?!.*\bjack\b.*\bjack\b|.*\bjames\b.*\bjames\b)(?=.*\bjack\b.*\bjames\b).*$
RegEx演示4
你可以使用regex的量词特性,因为不一定总是支持查找。
(\bjames\b){1,}.*(\bjack\b){1,}|(\bjack\b){1,}.*(\bjames\b){1,}
不需要两个lookhead,一个子字符串可以正常匹配。
^(?=.*?\bjack\b).*?\bjames\b.*
在regex101上查看这个演示
查找是零长度断言(条件)。这里的forward在^ start处检查是否jack出现在字符串的后面,如果成功则匹配到james和.*其余部分(可以删除)。Lazy dot用于单词之前(包含在\b单词边界中)。使用i标志来忽略大小写。