-5

I am unable to find issue with the format of this mail id , this is supposed to be according to the regular expression mentioned below:gt.1586@mail.dabur

^(([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?))$

Mansuro
  • 4,558
  • 4
  • 36
  • 76
  • 4
    Where is your code. What is your actual question? – hellow Apr 24 '18 at 08:29
  • I think your regex is not matching with the desired string email, if this is the case you can try - `^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$` – Tamaghna Banerjee Apr 24 '18 at 08:34
  • 1
    `[\w-\.]` should be `[-\w\.]` and so on: otherwise `-` would be taken as range operator like in `A-z`. – Joop Eggen Apr 24 '18 at 08:35
  • 1
    Your regex only allows the TLD to have 2-4 characters. "dabur" has 5 characters. Maybe you should find a regex that follows modern TLD rules. – RealSkeptic Apr 24 '18 at 08:36
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Clijsters Apr 24 '18 at 08:37
  • 2
    Your tag says Java. This means you don't need a regex for that. Also every time one uses regular expressions to validate mail addresses, a kitten gets killed. – Clijsters Apr 24 '18 at 08:37
  • What is the error? – Mark Rotteveel Apr 24 '18 at 08:47
  • @AxelH That depends on what the problem is. In any case, one really shouldn't validate domain names and tlds other than by actually sending an email. Validating tlds is the road to pain if new tlds get added (not to mention potential local-network validity of a hostname like `mail.dabur`). – Mark Rotteveel Apr 24 '18 at 08:50
  • @AxelH `.dabur` is a [valid TLD](http://data.iana.org/TLD/tlds-alpha-by-domain.txt) nowadays. – RealSkeptic Apr 24 '18 at 08:54
  • I agree @markrotteveel, I generally don't check more than the presence of "@" in a email, the rest will be validate by sending the first email. But since the question asked about validation the format of an email, the only logical answer would be to point to the RFC I believe (which I am looking for). – AxelH Apr 24 '18 at 08:55
  • Mmmh, it was update since my last validation method (a while ago)... Thanks for the information @RealSkeptic – AxelH Apr 24 '18 at 08:56
  • @AxelH This one: https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression ? – Mark Rotteveel Apr 24 '18 at 09:14
  • Yep, I am reading the [RFC5322](https://tools.ietf.org/html/rfc5322#section-3.4.1). I read in the past the [RFC822](http://www.ietf.org/rfc/rfc822.txt) when it was already obsolete. The comment in the linked question (not sure how I missed it) summarize what I would do to be honest ;) – AxelH Apr 24 '18 at 09:21

2 Answers2

2

You could try with below regular expression:

^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$

I hope this would help you.

Aditya Pawaskar
  • 243
  • 4
  • 11
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Clijsters Apr 24 '18 at 08:38
2

Your problem is with the [a-zA-Z]{2,4} section. This is trying to match the dabur section but failing because you are limiting the match to up to 4 characters and there are 5 in dabur.

Domain names used to be limited to 3 letters after the . and you have 5. This is no longer a restriction.

You can use [a-zA-Z]{2,7} or something similar but you may be better off removing that restriction entirely with [a-zA-Z]+.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • I had totally failed to see that. There doesn't seem to be a limit anymore indeed, the longest TLD I could see on http://data.iana.org/TLD/tlds-alpha-by-domain.txt is 24 characters. – Mark Rotteveel Apr 24 '18 at 09:12