我有几个电子邮件地址,'support@company.com'和'1234567@tickets.company.com'.
在perl中,我可以使用To:原始电子邮件的行,并找到上述任一地址
/\w+@(tickets\.)?company\.com/i
Run Code Online (Sandbox Code Playgroud)
在python中,我只是编写了上面的正则表达式,'\w+@(tickets\.)?company\.com'期望得到相同的结果.但是,support@company.com根本找不到,并且第二个上的findall返回仅包含的列表'tickets.'.很明显这'(tickets\.)?'是问题领域,但是我错过了Perl和Python之间正则表达式规则的区别究竟是什么?
文档re.findall:
Run Code Online (Sandbox Code Playgroud)findall(pattern, string, flags=0) Return a list of all non-overlapping matches in the string. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.
既然(tickets\.)是一个组,则findall返回而不是整个匹配.如果您想要整个匹配,请在整个模式周围放置一个组和/或使用非分组匹配,即
r'(\w+@(tickets\.)?company\.com)'
r'\w+@(?:tickets\.)?company\.com'
Run Code Online (Sandbox Code Playgroud)
请注意,findall在第一种情况下,您必须选择返回的每个元组的第一个元素.
| 归档时间: |
|
| 查看次数: |
1870 次 |
| 最近记录: |