【问题标题】:What does this regular expression maens "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/" [duplicate]这个正则表达式是什么意思 "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0 -9]{1,3})(\]?)$/" [重复]
【发布时间】:2018-08-27 00:07:22
【问题描述】:

发现了这种技术来验证电子邮件地址的格式是否正确。

function check_email($email) {  
        if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) || 
            (preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/',$email)) ) { 
             return true;
        } else {
             return false;
        }       
    }

我是 php 的新手。这个大的正则表达式命令是什么意思?

【问题讨论】:

  • 我建议看一下网上的正则表达式工具,你可以将正则表达式粘贴进去,它会解释每个步骤,例如regexr.com
  • 我在 regex101 上做了匹配。但我对“([?)”感到困惑是检查@之后是否有任何[或什么? php 分隔符之前的 ^ 和末尾的 $ 代表什么?
  • 这些模式一团糟。我建议你停止查看这个 sn-p,并开始搜索一些好的电子邮件验证正则表达式......或者更好地验证没有正则表达式的电子邮件地址 - 寻找:filter_var($email, FILTER_VALIDATE_EMAIL)

标签: php regex preg-match email-validation


【解决方案1】:

这个大的正则表达式命令是什么意思?

模式 #1 细分:

/           #start of pattern delimiter
(@.*@)      #Capture Group #1: match an @ sign, then zero or more (as many as possible) of any non-newline character, then another @ sign
|           #or
(\.\.)      #Capture Group #2: match a literal dot, then another literal dot
|           #or
(@\.)       #Capture Group #3: match an @ sign, then a literal dot
|           #or
(\.@)       #Capture Group #4: match a literal dot, then an @ sign
|           #or
(^\.)       #Capture Group #5: match the start of the string, then a literal dot
/           #end of pattern delimiter

在我看来,第一个模式看起来绝对是无用的垃圾。

模式 2 细分:

/                   #start of pattern delimiter
^                   #match start of string
.+                  #match any non-newline character one or more times (as much as possible)
\@                  #match @ (the \ is an escaping character which is not necessary)
(\[?)               #Capture Group #1: match an opening square bracket zero or one time
[a-zA-Z0-9\-\.]+    #match one or more (as much as possible) of the following characters: lowercase letters, uppercase letters, digits, hyphens, and dots (the \ before the . is an escaping character which is not necessary)
\.                  #match a literal dot
(                   #start Capture Group #2
  [a-zA-Z]{2,4}     #match any uppercase or lowercase letter 2, 3, or 4 times
  |                 #or
  [0-9]{1,3}        #match any digit 1, 2, or 3 times
)                   #end Capture Group #2
(\]?)               #Capture Group #3: match a closing square bracket zero or one time
$                   #match the end of the string
/                   #end of pattern delimiter

我不会推荐这些模式。

如果您想验证电子邮件,StackOverflow 周围有更好的模式,或者您可以使用 filter_var() 调用。

研究这个字符串:

filter_var($email, FILTER_VALIDATE_EMAIL)

【讨论】:

  • 请解释否决票。我说的话是不明智的还是不正确的?否决正确/信息丰富的答案有什么好处?如果您不喜欢这个问题,请对该问题投反对票。如果我的回答有什么问题值得一票否决,我想听听。
  • 我做了这个问题,我觉得您的回答很有用,先生。现在有些人说可能重复。但是不要认为他们有时理解这些文章不足以解释特定的正则表达式命令。我努力了。但是你知道,手上的所有手指都不等于你知道的。我是堆栈中的新用户,所以我不能投票。但非常感谢你的努力 mickmackusa。但我还有一个 qus。捕获组的目的是什么?并且任何电子邮件都有 [ 或 ] 这个标志,对吧?
  • @Hassan 没有更多的代表点,奖励有用的答案的唯一方法是奖励它的绿色大勾号(这些只能奖励给一个答案宠物页面)。我相信您的问题已关闭的原因是其他志愿者认为您的问题没有得到充分研究。两种模式中的捕获组用于隔离模式的某些部分,并避免与替代方案的意外错误 (|)。您的任务是关于验证(而不是提取),因此捕获组不会创建带有子模式匹配的臃肿输出。
  • 顺便说一句,我不知道方括号是否是电子邮件中的有效字符。我很确定我从未见过包含方括号的电子邮件。我敦促您使用电子邮件验证功能而不是正则表达式。
  • 当我设计和测试正则表达式模式时,我会访问 www.regex101.com,它是一个漂亮的工具,具有强大的功能,有效且具有教育意义。
猜你喜欢
  • 2010-12-11
  • 2015-01-07
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多