Extract email sub-strings from large document

Solution 1:

This code extracts the email addresses in a string. Use it while reading line by line

>>> import re
>>> line = "should we use regex more often? let me know at  [email protected]"
>>> match = re.search(r'[\w.+-]+@[\w-]+\.[\w.-]+', line)
>>> match.group(0)
'[email protected]'

If you have several email addresses use findall:

>>> line = "should we use regex more often? let me know at  [email protected] or [email protected]"
>>> match = re.findall(r'[\w.+-]+@[\w-]+\.[\w.-]+', line)
>>> match
['[email protected]', '[email protected]']

The regex above probably finds the most common non-fake email address. If you want to be completely aligned with the RFC 5322 you should check which email addresses follow the specification. Check this out to avoid any bugs in finding email addresses correctly.


Edit: as suggested in a comment by @kostek: In the string Contact us at [email protected]. my regex returns [email protected]. (with dot at the end). To avoid this, use [\w\.,]+@[\w\.,]+\.\w+)

Edit II: another wonderful improvement was mentioned in the comments: [\w\.-]+@[\w\.-]+\.\w+which will capture [email protected] as well.

Edit III: Added further improvements as discussed in the comments: "In addition to allowing + in the beginning of the address, this also ensures that there is at least one period in the domain. It allows multiple segments of domain like abc.co.uk as well, and does NOT match bad@ss :). Finally, you don't actually need to escape periods within a character class, so it doesn't do that."

Solution 2:

You can also use the following to find all the email addresses in a text and print them in an array or each email on a separate line.

import re
line = "why people don't know what regex are? let me know [email protected], [email protected] " \
       "[email protected],[email protected]"
match = re.findall(r'[\w\.-]+@[\w\.-]+', line)
for i in match:
    print(i)

If you want to add it to a list just print the "match"

# this will print the list
    print(match)

Solution 3:

import re
rgx = r'(?:\.?)([\w\-_+#~!$&\'\.]+(?<!\.)(@|[ ]?\(?[ ]?(at|AT)[ ]?\)?[ ]?)(?<!\.)[\w]+[\w\-\.]*\.[a-zA-Z-]{2,3})(?:[^\w])'
matches = re.findall(rgx, text)
get_first_group = lambda y: list(map(lambda x: x[0], y))
emails = get_first_group(matches)

Please don't hate me for having a go at this infamous regex. The regex works for a decent portion of email addresses shown below. I mostly used this as my basis for the valid chars in an email address.

enter image description here

Feel free to play around with it here

I also made a variation where the regex captures emails like name at example.com

(?:\.?)([\w\-_+#~!$&\'\.]+(?<!\.)(@|[ ]\(?[ ]?(at|AT)[ ]?\)?[ ])(?<!\.)[\w]+[\w\-\.]*\.[a-zA-Z-]{2,3})(?:[^\w])

Solution 4:

If you're looking for a specific domain:

>>> import re
>>> text = "this is an email [email protected], it will be matched, [email protected] will not, and [email protected] will"
>>> match = re.findall(r'[\w-\._\+%]+@test\.com',text) # replace test\.com with the domain you're looking for, adding a backslash before periods
>>> match
['[email protected]', '[email protected]']

Solution 5:

import re

reg_pat = r'\S+@\S+\.\S+'

test_text = '[email protected]    [email protected]   uiufubvcbuw bvkw  ko@com    m@urice'   

emails = re.findall(reg_pat ,test_text,re.IGNORECASE)
print(emails)

Output:

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