Why / ^ [a-zA-Z0-9] + @ [a-zA-Z0-9] \. (Com) | (edu) | (org) $ / i does not work properly

I have this regular expression for email authentication (valid only x@y.com , abc@defghi.org , something@anotherhting.edu )

/^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)|(edu)|(org)$/i

But @ abc.edu and abc@xyz.eduorg are both valid for the regex above. Can anyone explain why this is?

My approach:

  • must have at least one character or number before @

  • then comes @

  • There must be at least one character or number after @ and earlier.
  • the line should end with either edu, com, or org.
+4
source share
5

/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.(com|edu|org)$/i

- , "edu" , org. ,

  • ^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)
  • (edu)
  • (org)$

, . ! - . .

+3

:

/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.(com|edu|org)$/i

, i:

    /^[a-z0-9]+@[a-z0-9]+\.(com|edu|org)$/i

Regular expression visualization

N.B. + , , ...

+3

, , , :

[a-zA-Z0-9] + @[a-zA-Z0-9].com

edu

org

:

/^[a-z0-9]+@[a-z0-9]+\.(com|edu|org)$/i
+3

.

,

, , , Python

import re
pattern = re.compile ('^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)|(edu)|(org)$')
re.match('@abc.edu') # fails, use this to validate an input
re.search('@abc.edu') # matches, finds the edu
+2

: [A-Za-Z0-9] + @[A-Za-Z0-9] + ( | Edu | ). + $

U +, (com | edu | org)

: [a-zA-Z0-9] u +

-2
source

Source: https://habr.com/ru/post/1614307/