电子邮件验证表达式\w +([ - +。'] \w +)* @ \w +([ - 。] \w +)* \.\w +([ - 。] \w +)*空间 [英] Email validation expression \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* allows empty spaces
问题描述
电子邮件验证表达式 \w +([ - +。'] \w +)* @ \w +([ - 。] \w +)* \.\w +([ - 。] \w +)*
允许空格,否则可以完美地工作。
以下电子邮件地址不会失败:
john doe@hp.edu
我如何限制不允许使用空格?
某些API具有完整的功能如果是后一种情况,它不会失败,因为一个子字符串,特别是 doe@hp.edu
匹配模式。
要解决这个问题,您可以检查您使用的API具有匹配整个字符串的功能,或者您可以将 ^
添加到开头和 $
到您的模式,分别指示字符串/行的开始和结束。
^ \\ w +([ - +。'] \w +)* @ \w +([ - 。] \w +)* \.\w +([ - 。] \w +)* $
Email validation expression \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
allows empty spaces, but otherwise works perfectly.
It does not fail the following email address:
john doe@hp.edu
How can I restrict this to not allow whitespaces?
Some APIs have functionality to match on the entire string, where-as with others it would always search anywhere in the string.
If it's the latter case, it won't fail because a substring, specifically doe@hp.edu
, matches the pattern.
To fix this, you could either check if the API you're using has a function to match on the entire string, or you could add ^
to the start and $
to the end of your pattern, indicating the start and end of string / line respectively.
^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
这篇关于电子邮件验证表达式\w +([ - +。'] \w +)* @ \w +([ - 。] \w +)* \.\w +([ - 。] \w +)*空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!