DEV Community

Cover image for Email Pattern Matching with regex- NLP
datatoinfinity
datatoinfinity

Posted on

Email Pattern Matching with regex- NLP

In text preprocessing we were finding pattern of 'data-to-infinity' it is the same process but we will find email pattern like '[email protected]'.

So email contain special character, Lower case alphabet, upper case alphabet and digit or we can say alphanumeric value.

mail= [email protected]
pattern= \w]+@[\w]+.[\w]+

import re
mail='[email protected]'
pattern='[\w]+@[\w]+.[\w]+'
print(re.search(pattern,mail))
Output:

Pattern=[\w]+@[\w]+.[\w]
Group= [username]@[domain].[extension]

Basically we made pattern on basis that first username came in any email then domain like gmail,hotmail,yahoo etc then extension like .com,.in,.org.

If we use group() in search() the code will return the pattern string.

import re
mail='[email protected]'
pattern='[\w]+@[\w]+.[\w]+'
print(re.search(pattern,mail).group())
Output:
[email protected]

Email with Specific domain

import re
mail1='[email protected]'
mail2='[email protected]'
pattern='[\w]+@(gmail).(com)'
match1=re.search(pattern,mail1)
match2=re.search(pattern,mail2)
print(match1)
print(match2)
Output:
None

pattern='[\w]+@(gmail).(com)'

Email with multiple domain

import re
mail1='[email protected]'
mail2='[email protected]'
mail3='[email protected]'
pattern='[\w]+@(gmail).(com|in|org)'
match1=re.search(pattern,mail1)
match2=re.search(pattern,mail2)
match3=re.search(pattern,mail2)
print(match1)
print(match2)
print(match3)
None


pattern='[\w]+@(gmail).(com|in|org)'

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.