我有这样的字符串:
Alex Jatt, (alex.jatt@domain.com)
amd 我正在尝试使用正则表达式仅提取电子邮件地址,如下所示:
p = re.search('\((.*?)\)', c)
但 print p 命令打印(alex.jatt@domain.com)
如何修改此正则表达式以摆脱括号?
加入你也可以做到..
a= ''.join(c for c in a if c not in '()')
或使用正则表达式..
In[20]: import re
In[21]: name= re.sub('[()]', '', a)
In [22]: name
Out[22]: 'Alex Jatt, alex.jatt@domain.com'
没有regex
解决方案:
>>> strs="Alex Jatt, (alex.jatt@domain.com)"
>>> strs.split(',')[1].strip().strip("()")
'alex.jatt@domain.com'
re.search
允许您将匹配的组从正则表达式匹配中提取出来。在您的情况下,您可能希望用于p.group(1)
提取第一个带括号的匹配项,它应该是您拥有的正则表达式中的电子邮件。
use a look ahead and a look behind to make sure that the parenthesis are there, but to prevent you from capturing them.
p = re.search('(?<=\().*?(?=\))', c)
or you could just access the capture group instead of the whole regex.
p = re.search('\((.*?)\)', c).group(1)
either way would work.
I think you've been changing the code before pasting it in here.
If I do:
>>> import re
>>> c="Alex Jatt, (alex.jatt@domain.com)"
>>> p = re.search('\((.*?)\)', c)
>>> print p
<_sre.SRE_Match object at 0x10bd68af8>
You want to look at the groups:
>>> import re
>>> c="Alex Jatt, (alex.jatt@domain.com)"
>>> p = re.search('\((.*?)\)', c)
>>> print p.groups()[0]
alex.jatt@domain.com