20

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:

  1. First and last character has to be [a-z0-9]
  2. The username contents only [a-z0-9.]
  3. There cannot be consecutive periods (i.e: e..o@gmail.com [This is wrong])
  4. 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?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • 1
    Also, the part before the `@` has to be at least 6 **alphanumeric** (dots not included!) characters. – Daniel Kamil Kozar Apr 24 '13 at 19:57
  • 4
    If this is an assignment that *assumes* certain requirements for GMail addresses, that's ok -- but the requirements you state are not the ones GMail actually enforces. Double dots in the name are permitted (and ignored), as is a suffix starting with `+`. The name portion and the "gmail.com" domain name are case-insensitive, so uppercase letters are permitted. I don't know all the rules. What is your actual goal? (It's hard to think of an application where you'd want to validate gmail addresses and *only* gmail addresses.) – Keith Thompson Apr 24 '13 at 20:11
  • 2
    To expand on @KeithThompson 's reply. Your regular expression requirements satisfy the **sign up conditions** for gmail addresses. You are overlooking the fact that gmail allows you to add a + at the end of your username, and then any text after it you wish. It also allows you to add any number of dots (even right at the start/end of the username). It also accepts addresses with different capitalisation. Your bad specification makes the regex you asked for useless in the real world. You'd be better off with an [officially compliant regex](http://stackoverflow.com/a/201378/1208914) – stormCloud Apr 25 '13 at 11:11
  • 2
    My gmail address doesn't have gmail.com in it. – Quentin Jun 15 '13 at 09:42

10 Answers10

33

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.)

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • Matches `a@gmail.com` because of that `?`, is that intended? – Christopher Creutzig Apr 24 '13 at 20:05
  • @ChristopherCreutzig, no it wasn't. Corrected. Thanks! – Kijewski Apr 24 '13 at 20:07
  • Also, the `{4,}` counts the dots, too. See my answer for an alternative that doesn't and doesn't need lookahead. – Christopher Creutzig Apr 24 '13 at 20:10
  • @user2137179, please consider upvoting answers that did help you. (W. B. Reed's, alroc's …) – Kijewski Apr 24 '13 at 20:13
  • @user2137179 also Christopher did get the 6 alphanum chars thing right while I didn't. – Kijewski Apr 24 '13 at 20:15
  • @Kay I'm sorry I cannot vote for any one because my reputation is low. The expression you wrote `^[a-z0-9]([a-z0-9]|\.(?!\.)){4,30}[a-z0-9]@g(oogle)?mail\.com$` at first time is more suitable than Christopher's because Christopher's is matches this (i.e. `a.b.c.d.e.f.g.h.i.j.k.l.1.2.3.4.5.6.7.8.9@gmail.com`) –  Apr 25 '13 at 11:00
  • 5
    This answer is incomplete... someone+something@gmail.com is a valid gmail address... and this regex doesn't match it... this is my version: `^[a-z0-9]((\.|\+)?[a-z0-9]){5,}@g(oogle)?mail\.com$` – KnF Mar 17 '15 at 00:30
  • I've read the question... but still.. the question doesn't even consider a plus sign in the username part... if the OP uses your regex in an app and I try to register on it with myemail+some@gmail.com it won't accept my address, which is wrong, as this address is a valid one... – KnF Mar 17 '15 at 16:27
  • still accepting the multiple top level domains eg. xyz@gmail.com.com is valid but it shouldn't be – Ameed Faridi Feb 15 '23 at 08:02
  • 1
    @AmeedFaridi, it does not. You probably missed the trailing `$` when you copied the expression. – Kijewski Feb 16 '23 at 00:59
  • what could be general regex similar to this ? @kay – Ameed Faridi Feb 16 '23 at 19:21
  • @AmeedFaridi, it is best to open a new question when you have a question. Then others can join in the conversation. – Kijewski Feb 17 '23 at 17:39
12

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 :)

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • 1
    Thanks, but this isn't what I want. Your expression matches these examples (.aabb@gmail.com|aa..bb@gmail.com|aabb.@gmail.com) and this is wrong. –  Apr 24 '13 at 20:02
  • What for do you use the `\W`? – Kijewski Apr 24 '13 at 20:02
5
/([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.

  1. ([a-zA-Z0-9]+) - will match for first word which can have a-z, A-Z, and 0-9
  2. ([_.-{1}]) - will match _, -, . after first word
  3. ? - will match between 0(false) and 1(true) of the preceding token.
  4. ([a-zA-Z0-9]+) - will match for second word which can have a-z, A-Z, and 0-9
  5. \@ - will match special character @
  6. ([a-zA-Z0-9]+) - will match the word that is the domain name after @
  7. ([.]) - will match .
  8. ([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
  1. ([a-zA-Z0-9]+) - will match for first word which can have a-z, A-Z, and 0-9
  2. ([.{1}]) - will match . after first word
  3. ? - will match between 0(false) and 1(true) of the preceding token.
  4. ([a-zA-Z0-9]+) - will match for second word which can have a-z, A-Z, and 0-9
  5. \@ - will match special character @
  6. gmail - will match the word gmail that is the domain name after @
  7. ([.]) - will match .
  8. com - will match the final last word of the email id which will be com
Yogesh Chauhan
  • 348
  • 4
  • 11
  • 1
    Thanks Yogesh !!. changed gmail regex to **([a-zA-Z0-9]+)([\.{1}])?([a-zA-Z0-9]+)\@(?:gmail|GMAIL)([\.])(?:com|COM)** for accepting both upper and lower case – Karthikeyan Vellingiri Jan 21 '19 at 07:30
2

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.

Christopher Creutzig
  • 8,656
  • 35
  • 45
  • Thank you Christopher, but the expression that you've written `^[a-z0-9](\.?[a-z0-9]){5,29}@gmail\.com$` is matches this `a.b.c.d.e.f.g.h.i.j.k.l.1.2.3.4.5.6.7.8.9@gmail.com`and this isn't what I want. And that's why did I accepted Kay's answer. –  Apr 25 '13 at 10:59
  • You should accept whatever you think is right, of course – but apart from the fact that I didn't write `{5,29}` since I'd put that restriction in a second test, I'm curious as to how Kay's answer really is different from mine. As far as I can see, by now he copied the local part I had suggested, and his previous version did not check the presence of at least six non-dot characters. – Christopher Creutzig Apr 26 '13 at 07:11
1

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.

alroc
  • 27,574
  • 6
  • 51
  • 97
1

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, 'any@any.any')

output:
<re.Match object; span=(0, 11), match='any@any.com'>

vraj
  • 31
  • 1
  • 10
1

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

niceandsimple@gmail.com
very.common@gmail.com
disposable.style.email.with+symbol@gmail.com
other.email-with-dash@gmail.com

while it won't match any of those

invalid--email@gmail.com
invalid++email@gmail.com
invalid..email@gmail.com
-invalid-email@gmail.com
invalid--email-@gmail.com
Teodor
  • 81
  • 1
  • 11
0

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"

W. B. Reed
  • 1,614
  • 1
  • 10
  • 9
0

My solution for this is ^[a-zA-Z][-_.a-zA-Z0-9]{5,29}@g(oogle)?mail.com$

-3
([a-zA-Z0-9\.]{5,15})\@gmail[\.]com
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    This doesn't match the requirements, and exhibits many common (though as such mostly harmless) misconceptions about regular expressions. – tripleee Jan 05 '20 at 08:59