Valid email regex pattern
Here is a commonly used regular expression pattern to validate an email address:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Let me break it down:
^
matches the start of the string[a-zA-Z0-9._%+-]+
matches one or more of the following characters:- Letters (both uppercase and lowercase)
- Numbers
- Dot (.)
- Hyphen (-)
- Underscore (_)
- Percent (%)
- Plus sign (+)
@
matches the @ symbol[a-zA-Z0-9.-]+
matches one or more of the following characters:- Letters (both uppercase and lowercase)
- Numbers
- Dot (.)
- Hyphen (-)
\.
matches a period (.) character[a-zA-Z]{2,}
matches the domain extension (it must be at least 2 characters long and only contain letters)$
matches the end of the string
This pattern allows for the following:
- Local part (before the @ symbol): can contain letters, numbers, dot, hyphen, underscore, percent, and plus sign
- Domain name (after the @ symbol): can contain letters, numbers, dot, and hyphen
- Domain extension (after the last dot): must be at least 2 characters long and only contain letters
Note that this pattern does not validate whether the email address actually exists or is in use. It only checks the format of the email address.
Also, keep in mind that this pattern is not foolproof and may not catch all invalid email addresses. For example, it does not check for invalid characters in the local part or domain name, or for missing or extra characters.
If you need a more robust email validation, you may want to consider using a library or framework that provides email validation functionality.