re.findall 的匹配规则不能包含或关系,例如:
sentence = u"合同生效"
res = re.findall(u"合同(生效|履行).*?", sentence)
print(res)
# ['生效']
instruction = u"基金合同于2020年3月18日生效"
effective_date = re.findall(u"基金合同已?于\d{4}年\d{1,2}月\d{1,2}日正?式?生效", instruction)[0]
print(effective_date)
# 基金合同于2020年3月18日生效
effective_date = re.findall(u"基金合同已?于\d{4}年\d{1,2}月\d{1,2}日(正?式)?生效", instruction)[0]
print(effective_date)
# 可观察到打印结果为空
instruction = u"基金合同于2020年3月18日生效"
effective_date = re.findall(u"基金合同已?于\d{4}年\d{1,2}月\d{1,2}日正式生效|生效", instruction)[0]
print(effective_date)
# 生效
说明,"|"操作的是u"基金合同已?于\d{4}年\d{1,2}月\d{1,2}日正式生效" 和 u"生效"。
能用方括号 [],就不用圆括号加或 (|),比如 [%%﹪]【%%%%%%%】