0

I have created the following regex pattern in Golang flavor to catch valid / invalid email addresses: \b([\w\.-]{5,30})@[\w\.-]+\.([A-Za-z]{2,3})\b

There is one constraint, the following email address should be invalid as well : example@example.com so the only thing i could think of is adding this to the pattern: \b([\w\.-]{5,30})@[^e][^x][\w\.-]+\.([A-Za-z]{2,3})\b

The issue is that using golang flavor, you cant negate groups, which makes it much harder.I tried a to create a regex pattern and it caught too many False positives.

  • 1
    Why should `example@exmaple.com` be invalid? – icza May 25 '23 at 07:56
  • You don't want to just allow `exmaple@exmaple.com` or any address having `example.com` as domain (as it looks from your pattern attempt)? – logi-kal May 25 '23 at 07:57
  • Why are you using a regex for this? Regex is decidedly the _wrong_ tool for email validation. See [this answer](https://stackoverflow.com/a/1903368/13860) for a correct regex for email validation, then please consider another approach. – Jonathan Hall May 25 '23 at 08:00
  • 4
    For email validation, the standard lib has builtin solution, see [How to validate an email address in Go](https://stackoverflow.com/questions/66624011/how-to-validate-an-email-address-in-go/66624104#66624104) – icza May 25 '23 at 08:02
  • 2
    Its almost impossible to validate an email address via a regex. Don't do that. – Volker May 25 '23 at 08:10
  • This is an assignment i need to work on and find the best regex pattern.. One of the constrains is that exmaple@exmaple.com should be invalid. Any suggestions other than what i have created ? – daniel avihasira May 25 '23 at 08:12
  • The "best" pattern is hard to tell. The formally correct one is http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html. You probably won't like it and it may be impractical to translate to Go. A sensible starting point might be https://stackoverflow.com/a/201378/1266756. – Volker May 25 '23 at 08:33
  • 2
    But "assignment" can mean: "This is my homework and I should learn about complex regexps." or "This is my homework and I should experience that regexps are not the proper solution to a wide range of problems." or "Our legacy system just allows regexp validation and I have to find a suitable regexp for the type of email addresses we have". – Volker May 25 '23 at 08:35
  • Yes, this is some kind of homework, and i just need to avoid catching example@example.com using a better pattern... – daniel avihasira May 25 '23 at 08:45
  • Just a question about the original regex, why would `help@website.com` be invalid? Is it in your homework to keep this minimal length of 5 chars before the `@`? And is it `example` or `exmaple`? you seem to have multiple versions in your question. – Patrick Janser May 25 '23 at 11:37
  • Yes you are correct there was a typo.. its example@example.com . And yes i need to keep it minimal length of 5 chars before the @.. any suggestions ? – daniel avihasira May 25 '23 at 13:34

0 Answers0