-2

I have an authorizer function which needs to check email rules with wildcards e.g.

  • mike.12@*.myapp.com
  • p*@*.in
  • julia.admin@myapp.*

so e.g.

  • peter@gmail.com => false
  • peter@myapp.it => true (3rd. rule)
  • pete@someapp.in => true (2nd rule)

I am a bit stuck on how to best construct a regex - or maybe there are other out of the box solutions? Appreciate any help! Thanks

Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
Vinz
  • 87
  • 1
  • 6
  • 1
    Regex isn't the best way to go here. The full-up e-mail standard is ***phenomenally*** complicated. What platform are you on? Odds are there's a library that does it better than a roll-your-own regex ever could. – Sebastian Lenartowicz Jul 03 '17 at 20:56
  • What language/environment will you be implementing your regex in? – mickmackusa Jul 03 '17 at 23:22
  • Possible duplicate of [Using a regular expression to validate an email address](https://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – Daniel Beck Jul 04 '17 at 01:43

2 Answers2

0

Try this regex ([a-zA-Z0-9-_\.]*)\@(.*?.in|.*?myapp.*)
Demo https://regex101.com/r/D6thOr/1

lkdhruw
  • 572
  • 1
  • 7
  • 22
0

Pattern:

/^[^@]+@(?:(?:myapp\..+)|.+(?:\.in|myapp\.com))$/

Pattern Demo

Test Cases:

  • user@fail.com #fail
  • user@myapp.pass #pass
  • user@pass.in #pass
  • user@pass.myapp.com #pass

I used non-capturing groups, but you can use capture groups for pattern brevity if you wish.

I have ordered the "alternatives" with shorter mismatching patterns before longer ones for efficiency.

I also used a negated character class instead of a dot before @ for speed reasons.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136