正则表达式:是否有 AND 运算符? [英] Regular Expressions: Is there an AND operator?
问题描述
显然,您可以使用|
(管道?)来表示OR
,但是有没有一种方法也可以表示AND
?
Obviously, you can use the |
(pipe?) to represent OR
, but is there a way to represent AND
as well?
具体来说,我想匹配包含某个短语的所有内容的文本段落,但没有特定的顺序.
Specifically, I'd like to match paragraphs of text that contain ALL of a certain phrase, but in no particular order.
推荐答案
使用非消耗正则表达式.
Use a non-consuming regular expression.
典型的(即 Perl/Java)符号是:
The typical (i.e. Perl/Java) notation is:
(?=
expr)
这意味着匹配 expr 但之后在原始匹配点继续匹配."
This means "match expr but after that continue matching at the original match-point."
您可以根据需要做任意多的这些,这将是一个和".示例:
You can do as many of these as you want, and this will be an "and." Example:
(?=match this expression)(?=match this too)(?=oh, and this)
如果您需要在其中保存一些数据,您甚至可以在非消费表达式中添加捕获组.
You can even add capture groups inside the non-consuming expressions if you need to save some of the data therein.
这篇关于正则表达式:是否有 AND 运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!