Pattern matching email address using regular expressions
-
28-10-2019 - |
Question
Filter email address with regular expressions: I am new to regular expressions and was hoping someone might be able to help out.
I am trying to pattern match an email address string with the following format:
FirstName.LastName@gmail.com
I want to be sure that there is a period somewhere before the '@' character and that the characters after the '@' character matches gmail.com
Thanks, Brad
Solution
You want some symbols before and after the dot, so I would suggest .+\..+@gmail\.com
.
.+
means any symbols (.) can appear 1 or more times (+)
\.
means the dot symbol; screened with backslash to suppress the special meaning of .
@gmail
and com
should be matched exactly.
See also Regular Expression Basic Syntax Reference
EDIT: gmail rules for account name only allow latin letters, digits, and dots, so a better regex is
[a-zA-Z0-9]+\.[a-zA-Z0-9]+@gmail\.com
OTHER TIPS
check valid email
^(?=[a-zA-Z0-9][a-zA-Z0-9@.-]{5,253})^(?!.*?[.]{2})[a-zA-Z0-9.-]+[a-zA-Z0-9_-]@(?=.?[.]{1})(?!.?[-]{2})[a-zA-Z0-9][a-zA-Z0-9.-]+(.[a-z]{2,}|.[0-9]{1,})$
enforced rules:
- must start with alphanumeric char
- can only have alphanumeric and @._- char and have no max than 253 char
- cannot have 2 consecutives .
- char before @ can only be alphanumeric and ._-
- most have @ in the middle
- need to have at least 1 . in the domain part
- cannot have double - in the domain part
- can only have alphanumeric and .- char in the domain part
- need to finish by a valid extension of 2 or more letters
- added support for ip (test@1.1.1.1)
You don't even need regex since your requirements are pretty specific. Not sure what language you're using, but most would support doing a split on @
and checking for a .
. In python:
name, _, domain = email.partition('@')
if '.' in name and domain == 'gmail.com':
# valid
You haven't tell us what kind of regex flavor you need however this example will fit most of them:
.*\..*@gmail.com
Assuming Unix style where .
is any character: .*\..*@gmail\.com
Edit: escaped the .
in the domain