To simply validate that an entry looks like an email, you can use the following regex pattern.
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,7}\b
Note that this does not imply that the address is valid - it simply passes the most basic checks for it to resemble a valid address. This regex only validates that the address follows a valid email format:
\b
matches a word boundary to ensure that the email address is not part of a larger word.[A-Za-z0-9._%+-]+
matches one or more occurrences of alphanumeric characters, dot, underscore, percent, plus, or hyphen, representing the local part of the email address.@
matches the at symbol.[A-Za-z0-9.-]+
matches one or more occurrences of alphanumeric characters, dot, or hyphen, representing the domain name.\.
matches a literal dot (escaped with backslash).[A-Za-z]{2,7}
matches two to seven occurrences of alphabetic characters, representing the top-level domain (TLD).\b
matches a word boundary to ensure that the email address is not part of a larger word.
This pattern enforces the following rules for email addresses:
- The local part of the email address should contain at least one alphanumeric character, dot, underscore, percent, plus, or hyphen.
- The domain name should contain at least one alphanumeric character, dot, or hyphen.
- The TLD should contain two to seven alphabetic characters.
import re
text = "Please contact me at john.doe@example.com or info@example.org"
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,7}\b'
matches = re.findall(pattern, text)
print(matches)
In the example above, the re.findall()
function is used with the pattern to find all occurrences of email addresses in the given text. The matched email addresses are printed as a list.
def is_valid(email):
import re
pattern = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,7}$"
if re.match(pattern, email):
print("Valid Email")
else:
print('Not Valid')
is_valid('someone@somewhere.com')
is_valid('someone@some where.com')