Par*_*and 13 python regex email
给定一个电子邮件主题行,我想清理它,摆脱"Re:","Fwd"和其他垃圾.所以,例如,"[Fwd] Re:杰克和吉尔的婚礼"应该变成"杰克和吉尔的婚礼".
之前有人必须这样做,所以我希望你能指出我对经过测试的正则表达式或代码.
以下是此页面上的一些需要清理的示例.该页面上的正则表达式工作得相当好,但并不完全存在.
Fwd : Re : Re: Many
Re : Re: Many
Re : : Re: Many
Re:: Many
Re; Many
: noah - should not match anything
RE--
RE: : Presidential Ballots for Florida
[RE: (no subject)]
Request - should not match anything
this is the subject (fwd)
Re: [Fwd: ] Blonde Joke
Re: [Fwd: [Fwd: FW: Policy]]
Re: Fwd: [Fwd: FW: "Drink Plenty of Water"]
FW: FW: (fwd) FW: Warning from XYZ...
FW: (Fwd) (Fwd)
Fwd: [Fwd: [Fwd: Big, Bad Surf Moving]]
FW: [Fwd: Fw: drawing by a school age child in PA (fwd)]
Re: Fwd
Run Code Online (Sandbox Code Playgroud)
mat*_*fee 15
试试这个(替换为''):
/([\[\(] *)?(RE|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$/igm
Run Code Online (Sandbox Code Playgroud)
(如果你把每个主题作为自己的字符串,那么你不需要m修饰符;这只是为了$匹配行尾,而不仅仅是字符串的结尾,用于多行字符串输入).
在这里看到它.
正则表达式的解释:
([\[\(] *)? # starting [ or (, followed by optional spaces
(RE|FWD?) * # RE or FW or FWD, followed by optional spaces
([-:;)\]][ :;\])-]*|$) # only count it as a Re or FWD if it is followed by
# : or - or ; or ] or ) or end of line
# (and after that you can have more of these symbols with
# spaces in between)
| # OR
\]+ *$ # match any trailing \] at end of line
# (we assume the brackets () occur around a whole Re/Fwd
# but the square brackets [] occur around the whole
# subject line)
Run Code Online (Sandbox Code Playgroud)
标志.
i: 不区分大小写.
g:全局匹配(匹配您可以找到的所有Re/Fwd).
m:让正则表达式中的'$'匹配多行输入的行尾,而不仅仅是字符串的结尾(仅当您将所有输入主题一次输入正则表达式时才相关.如果每次输入一个主题,那么你可以将其删除,因为线的端部是字符串的结尾).
根据国家/语言的几种变体(主题前缀):维基百科:电子邮件主题缩写列表
巴西:RES === RE,德语:AW === RE
Python中的示例:
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import re
p = re.compile( '([\[\(] *)?(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|??|????|???|?????|?????|??|??|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$', re.IGNORECASE)
print p.sub( '', 'RE: Tagon8 Inc.').strip()
Run Code Online (Sandbox Code Playgroud)
PHP中的示例:
$subject = "??: Tagon8 - test php";
$subject = preg_replace("/([\[\(] *)?(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|??|????|???|?????|?????|??|??|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$/im", '', $subject);
var_dump(trim($subject));
Run Code Online (Sandbox Code Playgroud)
终奌站:
$ python test.py
Tagon8 Inc.
$ php test.php
string(17) "Tagon8 - test php"
Run Code Online (Sandbox Code Playgroud)
注意:这是mathematical.coffee的正则表达式.添加其他语言的其他前缀:中文,丹麦语挪威语,芬兰语,法语,德语,希腊语,希伯来语,意大利语,冰岛语,瑞典语,葡萄牙语,波兰语,土耳其语
我使用"strip/trim"来删除空格