为什么/^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)|(edu)|(org)$/i不能按预期工作

时间:2021-09-26 16:13:11

I have this regex for email validation (assume only x@y.com, abc@defghi.org, something@anotherhting.edu are valid)

我有这个用于电子邮件验证的正则表达式(假设只有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 as to the regex above. Can anyone explain why that is?

但是@ abc.edu和abc@xyz.eduorg对于上面的正则表达式都是有效的。谁能解释为什么会这样?

My approach:

  1. there should be at least one character or number before @

    在@之前应该至少有一个字符或数字

  2. then there comes @

    然后来了@

  3. there should be at least one character or number after @ and before .
  4. @和之前应该至少有一个字符或数字。

  5. the string should end with either edu, com, or org.
  6. 字符串应以edu,com或org结尾。

5 个解决方案

#1


3  

Try this

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

and it should become clear - you need to group those alternatives, otherwise you can match any string that has 'edu' in it, or any string that ends with org. To put it another way, your version matches any of these patterns

它应该变得清晰 - 你需要对这些替代品进行分组,否则你可以匹配任何包含'edu'的字符串,或任何以org结尾的字符串。换句话说,您的版本匹配任何这些模式

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

It's worth pointing out that the original poster is using this as a regex learning exercise. This would be a terrible regex for actual production use! It's a thorny problem - see Using a regular expression to validate an email address for a lot more depth.

值得指出的是,原始海报正在使用它作为正则表达式学习练习。这对于实际生产使用来说是一个可怕的正则表达式!这是一个棘手的问题 - 请参阅使用正则表达式来更深入地验证电子邮件地址。

#2


3  

Your grouping parentheses are incorrect:

您的分组括号不正确:

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

Can also just use one case as you're using the i modifier:

当您使用i修饰符时,也可以使用一个案例:

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

为什么/^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)|(edu)|(org)$/i不能按预期工作

N.B. you were also missing a + from the second set, I assume this was just a typo...

注:你也错过了第二盘的+,我认为这只是一个错字......

#3


3  

What you have written is the equivalent of matching something that:

你写的东西相当于匹配的东西:

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

以[a-zA-Z0-9] + @ [a-zA-Z0-9] .com开始

contains edu

or ends with org

或以组织结束

What you were looking for was:

你在寻找的是:

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

#4


2  

Your regex looks ok.

你的正则表达式看起来不错。

I guess you are looking using a find function in stead of a match function

我猜你正在寻找使用find函数而不是匹配函数

Without specifying what you use it is a bit difficult, but in Python you would write

没有指定你使用它有点困难,但在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

#5


-2  

Try to use it: [a-zA-Z0-9]+@[a-zA-Z0-9]+.(com|edu|org)+$

尝试使用它:[a-zA-Z0-9] + @ [a-zA-Z0-9] +。(com | edu | org)+ $

U forget about + modificator if u want to catch any combinations of (com|edu|org)

如果你想捕捉(com | edu | org)的任何组合,你会忘记+ modificator

Upd: as i see second [a-zA-Z0-9] u missed + too

更新:因为我看到第二个[a-zA-Z0-9]你错过了+

#1


3  

Try this

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

and it should become clear - you need to group those alternatives, otherwise you can match any string that has 'edu' in it, or any string that ends with org. To put it another way, your version matches any of these patterns

它应该变得清晰 - 你需要对这些替代品进行分组,否则你可以匹配任何包含'edu'的字符串,或任何以org结尾的字符串。换句话说,您的版本匹配任何这些模式

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

It's worth pointing out that the original poster is using this as a regex learning exercise. This would be a terrible regex for actual production use! It's a thorny problem - see Using a regular expression to validate an email address for a lot more depth.

值得指出的是,原始海报正在使用它作为正则表达式学习练习。这对于实际生产使用来说是一个可怕的正则表达式!这是一个棘手的问题 - 请参阅使用正则表达式来更深入地验证电子邮件地址。

#2


3  

Your grouping parentheses are incorrect:

您的分组括号不正确:

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

Can also just use one case as you're using the i modifier:

当您使用i修饰符时,也可以使用一个案例:

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

为什么/^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)|(edu)|(org)$/i不能按预期工作

N.B. you were also missing a + from the second set, I assume this was just a typo...

注:你也错过了第二盘的+,我认为这只是一个错字......

#3


3  

What you have written is the equivalent of matching something that:

你写的东西相当于匹配的东西:

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

以[a-zA-Z0-9] + @ [a-zA-Z0-9] .com开始

contains edu

or ends with org

或以组织结束

What you were looking for was:

你在寻找的是:

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

#4


2  

Your regex looks ok.

你的正则表达式看起来不错。

I guess you are looking using a find function in stead of a match function

我猜你正在寻找使用find函数而不是匹配函数

Without specifying what you use it is a bit difficult, but in Python you would write

没有指定你使用它有点困难,但在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

#5


-2  

Try to use it: [a-zA-Z0-9]+@[a-zA-Z0-9]+.(com|edu|org)+$

尝试使用它:[a-zA-Z0-9] + @ [a-zA-Z0-9] +。(com | edu | org)+ $

U forget about + modificator if u want to catch any combinations of (com|edu|org)

如果你想捕捉(com | edu | org)的任何组合,你会忘记+ modificator

Upd: as i see second [a-zA-Z0-9] u missed + too

更新:因为我看到第二个[a-zA-Z0-9]你错过了+