简体   繁体   English

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

[英]Python Regex sub() with multiple patterns

I'm wondering if there's any way to combine patterns with re.sub() instead of using multiples like below: 我想知道是否有任何方法可以将模式与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)

Desired output: 所需的输出:

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

If you're just trying to delete specific substrings, you can combine the patterns with alternation for a single pass removal: 如果您只是想删除特定的子字符串,则可以将模式与交替模式结合起来以进行单遍删除:

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)

It's more complicated if the "patterns" use actual regex special characters (because then you need to worry about wrapping them to ensure the alternation breaks at the right places), but for simple fixed patterns, it's simple. 如果“模式”使用实际的正则表达式特殊字符(因为您需要担心将它们包装起来以确保在正确的位置出现交替符)会更加复杂,但是对于简单的固定模式而言,这很简单。

If you had real regexes, rather than fixed patterns, you might do something like: 如果您有真正的正则表达式,而不是固定模式,则可以执行以下操作:

all_pats = [...]
combined_pat = r'|'.join(map(r'(?:{})'.format, all_pats))

so any regex specials remain grouped without possibly "bleeding" across an alternation. 因此,任何正则表达式的特殊功能都将保持分组,而不会在交替中“出血”。

You're not even using regular expressions so you may as well just chain replace : 您甚至没有使用正则表达式,因此也可以只进行链replace

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.'''

strip2 = s2.replace(s1, "").replace("Hours:","").strip()

print(strip2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM