我想知道是否有办法将模式与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
答案 0 :(得分:2)
如果您只是想删除特定的子字符串,可以将模式与轮换组合以便单次删除:
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))
所以任何正则表达式特殊字符都保持分组,而不会在交替中出现“流血”。
答案 1 :(得分:1)
你甚至没有使用正则表达式,所以你也可以链接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)