简体   繁体   中英

How to remove parenthesis using regex on Python?

I have string like this:

Alex Jatt, (alex.jatt@domain.com)

amd I'm trying to extract only email address using regex like so:

p = re.search('\((.*?)\)', c)

but print p command prints (alex.jatt@domain.com)

How can I modify this regex to get rid of parenthesis?

without regex solution:

>>> strs="Alex Jatt, (alex.jatt@domain.com)"
>>> strs.split(',')[1].strip().strip("()")
'alex.jatt@domain.com'

re.search allows you to pull matched groups out of the regular expression match. In your case, you would want to use p.group(1) to extract the first parenthesized match, which should be the email in the regular expression you have.

With join also you can do it..

a= ''.join(c for c in a if c not in '()')

or with regex..

In[20]: import re

In[21]: name= re.sub('[()]', '', a)

In [22]: name
Out[22]: 'Alex Jatt, alex.jatt@domain.com'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM