Regular Expression - Validate Gmail addresses
/([a-zA-Z0-9]+)([\_\.\-{1}])?([a-zA-Z0-9]+)\@([a-zA-Z0-9]+)([\.])([a-zA-Z\.]+)/g
This is the regular expression for the email addresses which will validate all the email addresses.
- ([a-zA-Z0-9]+) - will match for first word which can have a-z, A-Z, and 0-9
- ([_.-{1}]) - will match _, -, . after first word
- ? - will match between 0(false) and 1(true) of the preceding token.
- ([a-zA-Z0-9]+) - will match for second word which can have a-z, A-Z, and 0-9
- \@ - will match special character @
- ([a-zA-Z0-9]+) - will match the word that is the domain name after @
- ([.]) - will match .
- ([a-zA-Z.]+) - will match the final last word of the email id which can be com, co.in, org, etc..
But gmail does not allows other special characters to use so for the gmail email address the regular expression will be easier than this and will be as given below:
/([a-zA-Z0-9]+)([\.{1}])?([a-zA-Z0-9]+)\@gmail([\.])com/g
- ([a-zA-Z0-9]+) - will match for first word which can have a-z, A-Z, and 0-9
- ([.{1}]) - will match . after first word
- ? - will match between 0(false) and 1(true) of the preceding token.
- ([a-zA-Z0-9]+) - will match for second word which can have a-z, A-Z, and 0-9
- \@ - will match special character @
- gmail - will match the word gmail that is the domain name after @
- ([.]) - will match .
- com - will match the final last word of the email id which will be com
Simple regular expression for matching Gmail:
^[\w.+\-]+@gmail\.com$
Matches, if in the beginning of the string there is \w
(alphanumeric or underscore character) or .
or +
or -
, one or more times, followed by @gmail.com
in the end of the string.
You can test it in regexpal.
By the way, is there a documentation for Regular Expression?
Google is your friend :)
You did not tell which regex implementation you use.
^[a-z0-9](\.?[a-z0-9]){5,}@g(oogle)?mail\.com$
[a-z0-9]
first character(\.?[a-z0-9]){5,}
at least five following alphanumeric characters, maybe preceded by a dot (see @Daniel's comment, copied from @Christopher's answer)g(oogle)?mail
gmail or googlemail (see @alroc's answer)
Probably you will want to use case-insensitive pattern matching, too. (/.../i
in JavaScript.)
There's lots of documentation for regular expressions, but you'll have to make sure you get one matching the particular flavor of regex your environment has. Yes, there are numerous dialects. That being said “Mastering Regular Expressions” is, as far as I know, still the ultimate reference.
As to your specific question, I'd probably use
^[a-z0-9](\.?[a-z0-9]){5,}@gmail\.com$
Caveat: I didn't check if the rules you gave are correct. E-mail addresses in general certainly don't follow them.