Regular Expression - Check Gmail Addresses

I am trying to create an expression for checking Gmail addresses. This is what I have done so far.

`^ ([\ w] [\ w.] (?!.) @ gmail.com) I'm trying to create an expression for checking Gmail addresses. This is what I have done so far.

But it does not work as I want.

Gmail Address:

  • The first and last characters must be [a-z0-9]
  • Username only [a-z0-9.]
  • There can be no consecutive periods (ie: e..o@gmail.com [This is wrong])
  • The username must be between 6 and 30 letters long.

To be honest, I don't have much experience with regular expressions.

By the way, is there any documentation for regex?

+8
regex
source share
8 answers

You did not specify which regular expression implementation you are using.

 ^[a-z0-9](\.?[a-z0-9]){5,}@g(oogle)?mail\.com$ 
  • [a-z0-9] first character
  • (\.?[a-z0-9]){5,} at least five of the following alphanumeric characters may be preceded by a period (see @Daniel comment copied from @Christopher's answer)
  • g(oogle)?mail gmail or googlemail (see @alroc answer)

You may also need to use case-sensitive pattern matching. ( /.../i in JavaScript.)

+14
source share

Simple regex for Gmail mapping:

 ^[\w.+\-]+@gmail\.com$ 

Matches if there is a \w character (alphanumeric or underscore) or at the beginning of a line . or + or - , one or more times, followed by @gmail.com at the end of the line.

You can check it in regexpal .

By the way, is there any documentation for regex?

Google is your friend :)

+7
source share

There is a lot of documentation for regular expressions there, but you need to make sure that you get one appropriate special regular expression flavor that your environment has. Yes, there are numerous dialects. Saying “Mastering Regular Expressions”, as far as I know, is still the ultimate link.

As for your specific question, I would probably use

 ^[a-z0-9](\.?[a-z0-9]){5,}@gmail\.com$ 

Caution: I did not check the correctness of the rules you quoted. Email addresses generally do not follow them.

+1
source share

RFC 2822 indicates what a valid email address is, and this is discussed here . But, as this page notes, you cannot just accept it without reading and understanding what it is doing.

You have an advantage here, since you expect the address to always end with @gmail.com , which reduces the size of your regular expression (you can divide by @ and only check the first half).

BTW, GMail is not gmail.com worldwide - in the UK and Germany you will also find googlemail.com .

There is a lot of regular expression documentation around the Internet, but you should definitely read how the library / engine you use handles everything. There are slight differences between implementations.

+1
source share

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

+1
source share

Answering your other question:

This is a link to the regex documentation: http://www.regular-expressions.info/ There is more help for the specific language you use, just googleing "mylanguage regex"

0
source share
 /([a-zA-Z0-9]+)([\_\.\-{1}])?([a-zA-Z0-9]+)\@([a-zA-Z0-9]+)([\.])([a-zA-Z\.]+)/g 

This is a regular expression for email addresses that will check all email addresses.

  • ([a-zA-Z0-9] +) - will correspond to the first word, which may have az, AZ and 0-9
  • ([_.- {1}]) - will match _, - ,. after the first word
  • ? - will correspond to 0 (false) and 1 (true) of the previous token.
  • ([a-zA-Z0-9] +) - will correspond to the second word, which may have az, AZ and 0-9
  • \ @ - the special character @ will match
  • ([a-zA-Z0-9] +) - will correspond to the word, which is the domain name after @
  • ([.]) - will match.
  • ([a-zA-Z.] +) - will correspond to the last last word of the email identifier, which can be com, co.in, org, etc.

But gmail does not allow the use of other special characters for the gmail email address, the regular expression will be simpler than this, and will be indicated below:

 /([a-zA-Z0-9]+)([\.{1}])?([a-zA-Z0-9]+)\@gmail([\.])com/g 
  • ([a-zA-Z0-9] +) - will correspond to the first word, which may have az, AZ and 0-9
  • ([. {1}]) - will match. after the first word
  • ? - will correspond to 0 (false) and 1 (true) of the previous token.
  • ([a-zA-Z0-9] +) - will correspond to the second word, which may have az, AZ and 0-9
  • \ @ - the special character @ will match
  • gmail - will match the word gmail, domain name after @
  • ([.]) - will match.
  • com - will match the last last word of the email id that will be com
0
source share

Perhaps you can try this here Checking Gmail or Google Addresses http://regxer.com/post.php?post_id=20

-one
source share

All Articles