Regex to check valid email
Here is a commonly used regular expression 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)$
matches the end of the string
This regular expression will match most common email address formats, but keep in mind that it's not foolproof and may not match all valid email addresses. For example, it won't match email addresses with internationalized domain names (IDNs) or email addresses with non-ASCII characters.
Here are some examples of valid email addresses that this regular expression will match:
And here are some examples of invalid email addresses that this regular expression will not match:
- @example.com (missing local part)
- example..com (multiple dots in a row)
- [email protected] (missing domain)
- example@example (missing top-level domain)
If you need to validate email addresses in a specific context or according to specific requirements, you may want to consider using a more advanced regular expression or a dedicated email validation library.