电子邮件验证正则表达式在连字符上失败 [英] Email verification regex failing on hyphens

查看:91
本文介绍了电子邮件验证正则表达式在连字符上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下正则表达式验证电子邮件地址: ^。*(?=。{8,})[\w。] + @ [\w。] + [。 ] [a-zA-Z0-9] + $

I'm attempting to verify email addresses using this regex: ^.*(?=.{8,})[\w.]+@[\w.]+[.][a-zA-Z0-9]+$

它正在接受类似 a-bc @ def的电子邮件。 com ,但拒绝像 abc@de-f.com 这样的电子邮件(我在 http://tools.netshiftmedia.com/regexlibrary/ 进行测试)。

It's accepting emails like a-bc@def.com but rejecting emails like abc@de-f.com (I'm using the tool at http://tools.netshiftmedia.com/regexlibrary/ for testing).

可以有人解释为什么吗?

Can anybody explain why?

推荐答案

这是解释:



在您的常规表达式中,该部分与a-bc @ def.com 相匹配,而abc @ de-f.com [\w。] + [。] [a-zA-Z0-9] + $

Here is the explaination:

In your regualr expression, the part matches a-bc@def.com and abc@de-f.com is [\w.]+[.][a-zA-Z0-9]+$

这意味着:


  • 应该有一个或多个数字,单词字符(字母,数字和下划线)和空格(空格,制表符和换行符)或。。请参见引用 \w

后跟一个'。'

然后在集合<$ c $中跟随一个或多个字符c> a-zA-Z0-9 。

所以- de-f.com 与第一个 [\w。] + 格式不匹配)在规则1中。

So the - in de-f.com doesn't matches the first [\w.]+ format in rule 1.

您可以将此部分调整为 [ \w .-] + [。] [a-zA-Z0-9] + $ 。使-在@string中生效。

You could adjust this part to [\w.-]+[.][a-zA-Z0-9]+$. to make - validate in the @string.

这篇关于电子邮件验证正则表达式在连字符上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆