gpt4 book ai didi

python 多个正则表达式

转载 作者:行者123 更新时间:2023-11-28 16:45:41 25 4
gpt4 key购买 nike

我需要在一个字符串上应用多个正则表达式,我这样做是这样的:

regex = re.compile("...")
regex2 = re.compile("...")
regex3 = re.compile("...")
regex4 = re.compile("...")
if regex.match(string) == None and regex2.match(string) == None and regex3.match(string) == None and regex4.match(string) == None:

我想知道是否有另一种方法可以以某种方式合并或组合单个正则表达式,或者我是否已经以“正确的方式”进行了?

最佳答案

r_list = [re.compile("..."),
re.compile("..."),
re.compile("..."),
re.compile("...")]
if any(r.match(string) for r in r_list):
# if at least one of the regex's matches do smth

关于python 多个正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14100868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com