在任意顺序中匹配所有正则表达式条件

2024-04-26 09:36:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个网页,我想刮使用正则表达式。页面可能包含最多3个我关心的文本块。你知道吗

如果所有三个文本块都存在,那么它应该返回匹配,否则不返回匹配。文本可以按页面上的任何顺序排列。你知道吗

我试过了,但不符合“任何订单”的要求:

re_text = (Text block 1)((.|\n)*)(Text block 2)((.|\n)*)(Text block 3)
re_compiled = re.compile(re_text)

我应该在这里使用背景资料吗?还是有别的解决办法?你知道吗


Tags: text订单文本re网页页面block背景
2条回答

单独找他们怎么样?你知道吗

re_texts = [re.compile('textblock1'), re.compile('textblock2'), re.compile('textblock3')]

if all(r.search(text) for r in re_texts):
    # all matches found
>>> ('a' and 'b' and 'c') in 'xyz'
False
>>> ('a' and 'b' and 'c') in 'ayz'
True
>>> ('a' and 'b' and 'c') in 'abc'
True

相关问题 更多 >