'Regular Expression - Validate Gmail addresses
I am trying to create an expression to validate Gmail addresses. That's what I've done so far.
^([\w]*[\w\.]*(?!\.)@gmail.com)
I am trying to create an expression to validate Gmail addresses. That's what I've done so far.
But it isn't working as I want.
Gmail address:
- First and last character has to be [a-z0-9]
- The username contents only [a-z0-9.]
- There cannot be consecutive periods (i.e:
[email protected][This is wrong]) - There length of the username has to be between 6 and 30 letters.
Being honest I don't have much experience with the Regular Expressions.
By the way, is there a documentation for Regular Expression?
Solution 1:[1]
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)?mailgmail or googlemail (see @alroc's answer)
Probably you will want to use case-insensitive pattern matching, too. (/.../i in JavaScript.)
Solution 2:[2]
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?
Solution 3:[3]
/([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
Solution 4:[4]
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.
Solution 5:[5]
RFC 2822 specifies what constitutes a valid email address, and this is discussed here. But as that page notes, you can't just accept it without really reading through and understanding what it's doing.
You're at an advantage here, as you are expecting the address to always end in @gmail.com, which reduces the scope of your regex (you can split on the @ and only validate the first half).
BTW, GMail isn't gmail.com the world over - in the UK and Germany, you'll find googlemail.com as well.
There is lots of documentation on regular expressions all over the web, but you should make sure to read up on how the library/engine you're using handles things. There are slight variations between implementations.
Solution 6:[6]
Any mail you can use this regular experssion
([a-zA-Z0-9_.-]+)@([a-zA-Z]+)([\.])([a-zA-Z]+)
1)([a-zA-Z0-9_.-]+) - check a-z, A-Z, and 0-9, _, ., -
2)([a-zA-Z]+) - check a-z, A-Z, and 0-9
3) @ - will match special character @
4)([\.]) - dot(.) in regular expression have any character so if search dot(.) character then use [\.]
5) same step 2
Python
import re
pattern = r'([a-zA-Z0-9_.-]+)@([a-zA-Z]+)([\.])([a-zA-Z]+)'
re.match(pattern, '[email protected]')
output: <re.Match object; span=(0, 11), match='[email protected]'>
Solution 7:[7]
I found a solution that works very well
^[a-z0-9]+(?!.*(?:\+{2,}|\-{2,}|\.{2,}))(?:[\.+\-]{0,1}[a-z0-9])*@gmail\.com$
It will match following valid email addresses
[email protected]
[email protected]
[email protected]
[email protected]
while it won't match any of those
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Solution 8:[8]
In answering your other question:
This is a link to regex documentation: http://www.regular-expressions.info/ There is more help for the specific language you're using by just googleing "mylanguage regex"
Solution 9:[9]
My solution for this is ^[a-zA-Z][-_.a-zA-Z0-9]{5,29}@g(oogle)?mail.com$
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Yogesh Chauhan |
| Solution 4 | Christopher Creutzig |
| Solution 5 | alroc |
| Solution 6 | |
| Solution 7 | Teodor |
| Solution 8 | W. B. Reed |
| Solution 9 |
