gpt4 book ai didi

具有多种模式的 Python Regex sub()

转载 作者:行者123 更新时间:2023-12-04 23:18:35 25 4
gpt4 key购买 nike

我想知道是否有任何方法可以将模式与 re.sub() 结合使用,而不是使用如下倍数:

import re
s1 = "Please check with the store to confirm holiday hours."
s2 = ''' Hours:
Monday: 9:30am - 6:00pm
Tuesday: 9:30am - 6:00pm
Wednesday: 9:30am - 6:00pm
Thursday: 9:30am - 6:00pm
Friday: 9:30am - 9:00pm
Saturday: 9:30am - 6:00pm
Sunday: 11:00am - 6:00pm

Please check with the store to confirm holiday hours.'''

strip1 = re.sub(s1, '', s2)
strip2 = re.sub('\t', '', strip1)
print(strip2)

期望的输出:
Hours:
Monday: 9:30am - 6:00pm
Tuesday: 9:30am - 6:00pm
Wednesday: 9:30am - 6:00pm
Thursday: 9:30am - 6:00pm
Friday: 9:30am - 9:00pm
Saturday: 9:30am - 6:00pm
Sunday: 11:00am - 6:00pm

最佳答案

如果您只是想删除特定的子字符串,则可以将模式与交替组合以进行一次删除:

pat1 = r"Please check with the store to confirm holiday hours."
pat2 = r'\t'
combined_pat = r'|'.join((pat1, pat2))
stripped = re.sub(combined_pat, '', s2)

如果“模式”使用实际的正则表达式特殊字符(因为那么您需要担心包装它们以确保在正确的位置交替中断),则更复杂,但对于简单的固定模式,这很简单。

如果您有真正的正则表达式,而不是固定模式,您可能会执行以下操作:
all_pats = [...]
combined_pat = r'|'.join(map(r'(?:{})'.format, all_pats))

所以任何正则表达式特价都保持分组,而不会在交替中“流血”。

关于具有多种模式的 Python Regex sub(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642522/

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