简体   繁体   English

为什么电子邮件的此正则表达式不起作用

[英]Why does this regex for emails not work

I'm trying to make a regex to match email addresses, like any of these: 我正在尝试使用正则表达式来匹配电子邮件地址,例如以下任何一个:

example@website.com
first.last@website.org
joe87_smith@web.net

I've written this regex: 我写了这个正则表达式:

$pattern = "/[\-\.\_a-z0-9]+(\@){1}[\-\.\_a-zA-Z0-9]+(\.){1}[\-a-z0-9]+/i";

and here is some code that I am using to test it: 这是一些我用来测试的代码:

$str = "test_last@test.com was the email address associated with another one, another.test@other.org";
$pattern = "/[\-\.\_a-z0-9]+(\@){1}[\-\.\_a-zA-Z0-9]+(\.){1}[\-a-z0-9]+/i";
preg_match_all($pattern, $str, $matches);
var_dump($matches);

(The text between the emails is filler) It's supposed to do as follows: (电子邮件之间的文本为填充符)应该执行以下操作:

  1. Check for a username that can include one or more periods, dashes, underscores, or alphanumeric characters. 检查用户名可以包含一个或多个句点,破折号,下划线或字母数字字符。
  2. Check for one and only one (required) "@" sign. 检查一个且只有一个(必需)“ @”符号。
  3. Check for a domain or any number of subdomains (alphanumeric + periods + dashes) 检查一个域或任意数量的子域(字母数字+句号+破折号)
  4. Check for a period followed by alphanumeric or dash characters. 检查后跟字母数字或破折号的句点。

When I test the code above, I get this output: 当我测试上面的代码时,我得到以下输出:

array(3) {
    [0] => array(2) {
        [0] => string(22) "test_last@test.com was"
        [1] => string(22) "another.test@other.org"
    }
    [1] => array(2) {
        [0] => string(1) "@"
        [1] => string(1) "@"
    }
    [2] => array(2) {
        [0] => string(1) " "
        [1] => string(1) "r"
    }
 }

Why is it matching so many other characters, such as single @ signs and the letter "r"? 为什么它与这么多其他字符匹配,例如单个@符号和字母“ r”? Why does the very first email contain the word was? 为什么第一封电子邮件中包含单词was? I never tested for spaces to my knowledge... 据我所知,我从未测试过空间。

To answer the question from the comments. 从评论中回答问题。 The problem was using groups within regex which means that preg_match_all was matching on those groups separately as well. 问题是在正则表达式中使用组,这意味着preg_match_all也分别在这些组上匹配。

Changing the regex to: 将正则表达式更改为:

/[\-\.\_a-z0-9]+[\@]{1}[\-\.\_a-zA-Z0-9]+[\.]{1}[\-a-z0-9]+/

Returned: 回来:

Array
(
    [0] => Array
        (
            [0] => test_last@test.com
            [1] => another.test@other.org
        )

)

Using the OPs test text. 使用OP测试文本。

PHP has built in filters to check for things like e-mail validity now. PHP内置了过滤器,现在可以检查诸如电子邮件有效性之类的内容。 More specifically, you might want to look into filter_var() and the FILTER_VALIDATE_EMAIL filter. 更具体地说,您可能需要研究filter_var()FILTER_VALIDATE_EMAIL过滤器。

Sample usage: 用法示例:

$valid_email = filter_var($email, FILTER_VALIDATE_EMAIL);
if($valid_email)
        echo "Hooray!";

All three of your sample e-mail addresses should return the "hooray!" 您的所有三个示例电子邮件地址都应返回“万岁!”

Validating email addresses (with regexp and otherwise) is problematic; 验证电子邮件地址(使用regexp和其他方式)是有问题的; see here: Using a regular expression to validate an email address . 请参阅此处: 使用正则表达式来验证电子邮件地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM