Simple regex pattern for email [duplicate]
Asked Answered
C

6

5

I've been trying to work this out for almost an hour now, and I can't see myself getting much further with it without any help or explanation. I've used regex before, but only ones that are very simple or had already been made.

This time, I'm trying to work out how to write a regex that achieves the following:

Email address must contain one @ character and at least one dot (.) at least one position after the @ character.

So far, this is all I've been able to work out, and it still matches email addresses that, for example, have more than one @ symbol.

.*?@?[^@]*\.+.*

It would be helpful if you can show me how to construct a regular expression that checks for a single @ and at least one full stop one or more spaces after the @. If you could break down the regex and explain what each bit does, that would be really helpful.

I want to keep it simple for now, so it doesn't have to be a full-on super-accurate email validation expression.

Chane answered 14/5, 2018 at 12:17 Comment(7)
Are you planning on using this in production? Why no "full-on super-accurate email validation"?Koffler
Probably this one can give you a hint.Neurogram
@Luca No, I'm not. It's part of a small JavaScript project for practice. I want to keep the validation simple so I can learn regex more easily.Chane
use this link.. will help you creating regex.. regexr.comShiah
I'd also take a look at this answerKoffler
This satisfies your requirement: [^@]+@[^@]+\.[^@]+ (and it adds the requirement of at least one character before the @ and one between the @ and the .).Onagraceous
@ClasG Thank you so much! Just what I was after.Chane
C
21

With the help of ClasG's comment, I now have a fairly straightforward and suitable regex for my problem. For the sake of anyone learning regex who might come across this question in the future, I'll break the expression down below.

Expression: ^[^@]+@[^@]+\.[^@]+$

  • ^ Matches the beginning of the string (or line if multiline)
  • [^@] Match any character that is not in this set (i.e. not "@")
  • + Match one or more of this
  • @ Match "@" character
  • [^@] Match any character that is not in this set
  • + Match one or more
  • \. Match "." (full stop) character (backslash escapes the full stop)
  • [^@] Match any character that is not in this set
  • + Match one or more
  • $ Matches the end of the string (or line if multiline)

And in plain language:

  • Start at beginning of string or line
  • Include all characters except @ until the @ sign
  • Include the @ sign
  • Include all characters except @ after the @ sign until the full stop
  • Include all characters except @ after the full stop
  • Stop at the end of the string or line
Chane answered 15/5, 2018 at 5:40 Comment(3)
Based on this approach, if you need to support comma separated emails, you can use ^[^@]+@[^@]+\.[^@]+(;[^@]+@[^@]+\.[^@]+)*$. Note that these regexes also validate emails in format Name Lastname <[email protected]> (not in a smart way, but because it's a simple regex). It would validate invalid stuff like a<sd@as>ds.com, but that's probably acceptable because you just want to make sure the user doesn't enter total garbage or something like a phone by mistake.Rescission
This is completely wrong format because. email id: abcd#[email protected] allow hereHerwick
This will also match a full line like this: "Contact me at [email protected]" This solution assumes that the line contains nothing but the e-mail.Katrinka
M
6

Email address must contain one @ character

No they don't. An email address with no '@' character is perfectly valid. An email address with multiple '@' characters before an IP address is perfectly valid (as long as all but 1 are outside the ADDR_SPEC or are quoted/escaped within the mailbox name).

I suspect you're not trying to validate an email address but rather an ADDR_SPEC. The answer linked by Máté Safranka describes how to validate an ADDR_SPEC (not an email address). Unless you expect to be validating records which don't have a valid internet MX record, and more than one '@' is more likely be a typo than a valid address....

/[a-z0-9\._%+!$&*=^|~#%'`?{}/\-]+@([a-z0-9\-]+\.){1,}([a-z]{2,16})/
Manicurist answered 14/5, 2018 at 12:36 Comment(0)
S
1

A validation for a standard email can be done using the following expression

Acceptable email prefix (before @) formats

  • Allowed characters: letters (a-z), numbers, underscores, periods, and dashes.
  • An underscore, period, or dash must be followed by one or more letter or number.

Acceptable email domain (after @) formats

  • Allowed characters: letters, numbers, dashes.
  • The last portion of the domain must be at least two characters, for example: .com, .org, .cc

Expression: ^[a-zA-Z0-9]+([._-][0-9a-zA-Z]+)*@[a-zA-Z0-9]+([.-][0-9a-zA-Z]+)*\.[a-zA-Z]{2,}$

  • ^: Matches the beginning of the string
  • [a-zA-Z0-9]+: Matches one or more (+) letters and numbers
  • ([._-][0-9a-zA-Z]+)*: Optional block (*), Matches any single "._-" followed by at least a letter or number
  • @: Matches "@"
  • [a-zA-Z0-9]+: Matches one or more (+) letters and numbers
  • ([.-][0-9a-zA-Z]+)*: Optional block (*), Matches any single ".-" followed by at least a letter or number
  • \.: Matches "." (it needs to be escaped)
  • [a-zA-Z]{2,}: Matches two or more ({2,}) letters
  • $: Matches the end of the string

If you want to accept only lowercase, use "a-z" only instead "a-zA-Z" If you want to limit the number of chars (17 for example) after the last ".", use {2,17}

Sapota answered 14/5 at 17:12 Comment(0)
C
0

^[^\W_]+\w*(?:[.-]\w*)*[^\W_]+@[^\W_]+(?:[.-]?\w*[^\W_]+)*(?:\.[^\W_]{2,})$

Email Regex

Cheddite answered 10/11, 2022 at 22:12 Comment(6)
This fails for [email protected]Bonzer
I updated the Regex. This one should cover the most basic cases.Cheddite
fails for an email address with only one character in the local portion, like [email protected]Loren
@Cheddite How you get that image ? Can you attaches the website url?Herwick
@Herwick jex.im/regulex/#!flags=&re=Cheddite
After lots of search I already found that URL. Thank you very much @Zombievirus, to show the easy understanding of reagex. Now I'm use this tools frequently for my understanding & learning. Thank you again.Herwick
H
0

Valid email format:

/^[a-z0-9][\w\.]+\@\w+?(\.\w+){1,}$/gi

or

/^[a-z0-9][\w\.]{m,n}\@\w+?(\.\w+){1,}$/gi

m  → minimum characters of email username
n  → maximum characters of email username
^[a-z0-9]  → first character always start with letter or numbers
[\w\.]+    → for second character allow letters, numbers, _ & .; one or more times. 
              Or you may use [\w\.]{m,n}, to define min & max of email username length.
\@         → Then allow '@' character
\w+?       → must match email domain name one or more characters
(\.\w+){1,} → should match top-level domain name. It may be '.com' or '.co.in' like that. So, use {1, }

I found few issue when trying to generate email id.
So, create regex accordingly that.

enter image description here enter image description here

Herwick answered 14/5 at 19:0 Comment(0)
S
0

This is simple email regex pattern

^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$

Another email regex

^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$
Sisak answered 27/9 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.