Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove parenthesis using regex on Python?

Tags:

python

regex

I have string like this:

Alex Jatt, ([email protected])

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

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

but print p command prints ([email protected])

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

like image 652
Sushan Ghimire Avatar asked Feb 17 '26 01:02

Sushan Ghimire


2 Answers

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.

like image 178
murgatroid99 Avatar answered Feb 18 '26 15:02

murgatroid99


without regex solution:

>>> strs="Alex Jatt, ([email protected])"
>>> strs.split(',')[1].strip().strip("()")
'[email protected]'
like image 38
Ashwini Chaudhary Avatar answered Feb 18 '26 15:02

Ashwini Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!