1

I have the following string:

'[email protected], [email protected], [email protected], [email protected]'

I want to extract com, gov, com, bi.

I wrote (?<=@)\w+ regex, but it turn out to extract yahoo,est,ywuancjds,ililsa. I don't know how to include the vocabulary behind @.

1
  • Regarding est.gov.cn -> gov. Do you want a.b.c.d to return only c then too? Commented Apr 2, 2020 at 11:08

1 Answer 1

2

You may use

re.findall(r'@[^\s.]+\.(\w+)', text)

See the regex demo and the Python demo

Details

  • @ - a @ char
  • [^\s.]+ - 1 or more chars other than whitespace and a dot
  • \. - a dot
  • (\w+) - Group 1 (the value captured in this group will only be returned by re.findall): one or more word chars.

Python demo snippet:

import re
text = "[email protected], [email protected], [email protected], [email protected]"
print( re.findall(r"@[^\s.]+\.(\w+)", text) )
# => ['com', 'gov', 'com', 'bi']
Sign up to request clarification or add additional context in comments.

2 Comments

I am also wondering if I only want the email address in odd order, how could I write it?
@user13196723 Sorry, not sure what you mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.