问题描述
我正在尝试查找包含单词"text1"和"text2"的所有页面.我的正则表达式:
I'm trying to find all pages which contain words "text1" and "text2".My regex:
text1(.|\n)*text2
它不起作用..
推荐答案
如果您的IDE支持s
(单行)标志(因此.
字符可以匹配换行符),您可以使用以下方法搜索商品:
If your IDE supports the s
(single-line) flag (so the .
character can match newlines), you can search for your items with:
(text1).*(text2)|\2.*\1
Example with s
flag
如果IDE不支持s
标志,则需要使用[\s\S]
代替.
:
If the IDE does not support the s
flag, you will need to use [\s\S]
in place of .
:
(text1)[\s\S]*(text2)|\2[\s\S]*\1
某些语言使用$1
和$2
代替\1
和\2
,因此您可能需要更改它们.
Some languages use $1
and $2
in place of \1
and \2
, so you may need to change that.
或者,如果您只想匹配一个文件包含两个字符串(但实际上不选择任何内容),则可以使用超前:
Alternately, if you want to simply match that a file contains both strings (but not actually select anything), you can utilize look-aheads:
(?s)^(?=.*?text1)(?=.*?text2)
这与参数的顺序(或数字)无关,对于您要搜索的每个其他文本,您只需附加另一个(?=.*?text_here)
.这种方法很好,因为您甚至可以包含正则表达式,而不仅仅是纯字符串.
This doesn't care about the order (or number) of the arguments, and for each additional text that you want to search for, you simply append another (?=.*?text_here)
. This approach is nice, since you can even include regex instead of just plain strings.
这篇关于正则表达式在页面上找到两个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!