2

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

Toto
  • 89,455
  • 62
  • 89
  • 125
Brad
  • 43
  • 1
  • 1
  • 4

6 Answers6

4

check valid email

^(?:(?!.*?[.]{2})[a-zA-Z0-9](?:[a-zA-Z0-9.+!%-]{1,64}|)|\"[a-zA-Z0-9.+!% -]{1,64}\")@[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
  • cannot have 2 consecutives . exept for quoted string
  • char before @ can only be alphanumeric and ._-%, exept for quoted string
  • must 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
  • support IP address (test@1.1.1.1)
  • support for quoted user name
wyzeman
  • 89
  • 4
  • 1
    FYI, it yells that `test@gm.` is a valid email, which is not – Thomas Ayoub Jun 19 '18 at 07:38
  • fixed that and some other minor bug – wyzeman Jul 30 '18 at 14:30
  • 1
    Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Feb 13 '21 at 09:14
  • thank you for the tips, I have made a few adjustements according to valid and invalid link listed in the wikipedia link you provide. – wyzeman Feb 15 '21 at 15:39
  • There is a slash missing at the domain part before the ., thus it accepts e.g. two @ or any character in the domain name. Here is a proposed fix:^(?:(?!.*?[.]{2})[a-zA-Z0-9](?:[a-zA-Z0-9.+!%-]{1,64}|)|\"[a-zA-Z0-9.+!% -]{1,64}\")@[a-zA-Z0-9][a-zA-Z0-9.-]+\.(?:[a-z]{2,}|[0-9]{1,})$ – Alexander Jährling Jul 28 '23 at 09:03
4

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

Alexey Kukanov
  • 12,479
  • 2
  • 36
  • 55
  • just to add; the `+`'s are necessary outside of your specifications @Brad in order to be a valid email (they can't start or end with a `.`) with that said though email regex get really complicated because of the specifications for valid addresses. – Jordan May 02 '11 at 19:35
  • Thanks very much! It works as expected and I appreciate you giving me the definitions. I will try to figure out matching the following phone number string next. (111) 111-1111 – Brad May 02 '11 at 19:40
  • with such a regex, an email such as ∂øøµ$∂å¥!@gmail.com will be valid. Is there a more precise and secure regex to fix this? – Igbanam May 16 '11 at 17:29
  • @Yasky: sure; I added it to the answer. Thanks for pointing that out! – Alexey Kukanov May 17 '11 at 06:07
0

Assuming Unix style where . is any character: .*\..*@gmail\.com

Edit: escaped the . in the domain

Jordan
  • 4,928
  • 4
  • 26
  • 39
0

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
Dan Breen
  • 12,626
  • 4
  • 38
  • 49
0

You haven't tell us what kind of regex flavor you need however this example will fit most of them:

.*\..*@gmail.com
hmontoliu
  • 3,960
  • 1
  • 19
  • 21
0

I used follwing regex expression to validate the email address. Also I have added a small code snippet in C# language regarding to that.

Regex - "^[a-zA-Z0-9]{3,20}@gmail.com$"

Code :-

static void Main(string[] args)
        {
            Console.WriteLine("Enter Your Text ");
            string input = Console.ReadLine();
            Console.WriteLine("The Text that you have entered is :" + input);
            Console.ReadLine();

            string pattern = "^[a-zA-Z0-9]{3,20}@gmail.com$";
            Regex regex = new Regex(pattern);
            bool results = regex.IsMatch(input);
            Console.WriteLine(results.ToString());
            Console.ReadLine();
        }

Emails such as ∂øøµ$∂å¥!@gmail.com also checked and show as false here. ;-)

  • Seems like this won't match the period that the OP specifies. Maybe you could also clarify why it only matches between 3 and 20 characters – camille Dec 22 '20 at 18:50
  • {3,20} means, the input text should have to have 3 to 20 number characters. Since this problem is regarding email validation, I added that as a more step of validation. – namal wijekoon Dec 23 '20 at 16:57
  • Right, but that means it will fail to match really long email addresses. It also will fail on the type of email address the OP asked about, where there's a period – camille Dec 23 '20 at 20:17
  • 1. Fail to match really long email addresses. :- you can edit **{3,20}** as **{3,}** 2. Period :- use this regex for the entire validation **^[a-z0-9A-Z]{1,}[.][a-z0-9A-Z]{1,}@gmail.com$** And sorry for the delayed reply. – namal wijekoon Dec 28 '20 at 09:07