以下正则表达式将以非常安全的方式匹配 gmails 前缀。它确保有 3 个逗号和升文本 On ... 写道
On([^,]+,){3}.*?wrote:
如果正则表达式应该以不区分大小写的方式匹配,那么不要忘记添加修饰符。
if re.search("On([^,]+,){3}.*?wrote:", subject, re.IGNORECASE):
# Successful match
else:
# Match attempt failed
亲切的问候,巴克利
Match the characters “On” literally «On»
Match the regular expression below and capture its match into backreference number 1 «([^,]+,){3}»
Exactly 3 times «{3}»
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «{3}»
Match any character that is NOT a “,” «[^,]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “,” literally «,»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the characters “wrote:” literally «wrote:»
Created with RegexBuddy