0

I know there are multiple discussions about the topic of matching email format. Examples are:

Despite of this I was not able to copmplete my Python Regex in a way that it matches every example which I have collected. In this demo you can find all possible valid and not valid email formats with focus on Germany (.de), Austria (.au) and international (.com).

The best regex to match my examples so far is:

^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$

EDIT: Based on fist user comments many user say my demo is to complexe. Here is a more simple one demo.

PParker
  • 1,419
  • 2
  • 10
  • 25
  • did you try to long regex as well? – Mahrkeenerh Nov 19 '21 at 12:20
  • No, what do you mean with that? – PParker Nov 19 '21 at 12:25
  • ```[a-zA-Z0-9_\-]{1,20}@[a-zA-Z0-9]{1,20}\.[a-zA-Z]{2,3}``` – Jerry An Nov 19 '21 at 12:26
  • this answer has a long regex that should solve it for ya: https://stackoverflow.com/a/1903368/13000953\ – Mahrkeenerh Nov 19 '21 at 12:26
  • best validation is to send an email with secret code that they need to respond to ;-) – mama Nov 19 '21 at 12:26
  • @Mahrkeenerh Yes, I have tried it. It doesn't work. – PParker Nov 19 '21 at 12:29
  • @JerryAn As you can see in my demo, you solution doesn't match all cases. – PParker Nov 19 '21 at 12:30
  • Match all cases is impossible, you should keep a balance between valid cases and invalid cases @PParker – Jerry An Nov 19 '21 at 12:32
  • @Mahrkeenerh It doesn't work at all: https://regex101.com/r/MZSYVV/1 – PParker Nov 19 '21 at 12:33
  • 1
    Looking at your demo data, why are `_test@gmail.com` or `t.e.st@gmail.com` invalid ? – Maurice Meyer Nov 19 '21 at 12:34
  • That's an issue with your checker, that it requires to escape all the quotation marks – Mahrkeenerh Nov 19 '21 at 12:35
  • @MauriceMeyer Ah, you think these are valid emails? Okay, then I will change my demo in my first post. Thank you very much! – PParker Nov 19 '21 at 12:37
  • @PParker: Why at all, do you want to match them ? – Maurice Meyer Nov 19 '21 at 12:40
  • Well, my chatbot is validating email addresses from customers in Germany and Austria. I thought these would be the most common cases for email addresses for people in these countries. Is this too much? Is this unrealistic? – PParker Nov 19 '21 at 12:44
  • I have added a more simple demo – PParker Nov 19 '21 at 12:49
  • 1
    Why is `email@example.web` invalid? – Wiktor Stribiżew Nov 19 '21 at 12:50
  • well, you think it is valid? I will change it! – PParker Nov 19 '21 at 12:55
  • @WiktorStribiżew You are the master of regex. You think it is possible to find a perfectly matching regex for my master demo? Or should we stick with the simplified demo? – PParker Nov 19 '21 at 13:00
  • 4
    The problem with your question is that it is example-based only. Regex is a rule-based thing, you should provide exact requirements. Else, "lemme-change-the-examples-and-it-will-work" makes little sense for future visitors. https://regex101.com/r/7Wrloe/1 will work, I think, for these examples. But with that demo, I am answering another question already. – Wiktor Stribiżew Nov 19 '21 at 13:02
  • Ok, more rules, less examples. I will remember it for the future. If you think your approache is good, I will take it. Looks good to me on the first glance! – PParker Nov 19 '21 at 13:07
  • 1
    Looking at https://en.wikipedia.org/wiki/Email_address#Local-part it appears that the problem is a little bit more complex than what you examples tend to show. – hpchavaz Nov 19 '21 at 13:53
  • @hpchavaz Thank you very much for your link. Email addresses are much more complexe than I thought. Very interesting! – PParker Nov 19 '21 at 14:08
  • @PParker, and what seems weird can be useful. For instance Google gmail accepts adresses in the form "name+xxx@gmail.com" for the account "name@gmail.com" : the 'xxx' can then be use to filter incoming emails , but unfortunately many sites do not accept these types of address. – hpchavaz Nov 19 '21 at 14:16

0 Answers0