C 中的常规 exp 验证电子邮件

发布于 2024-09-04 02:27:15 字数 2552 浏览 6 评论 0原文

我们需要用 C 语言编写一个电子邮件验证程序。我们计划使用 GNU Cregex.h)正则表达式。

我们准备的正则表达式是

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

但是下面的代码在编译正则表达式时失败。

#include <stdio.h>
#include <regex.h>

int main(const char *argv, int argc)
{

    const char *reg_exp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";

int status = 1;

    char email[71];

    regex_t preg;

    int rc;

    printf("The regex = %s\n", reg_exp);

    rc = regcomp(&preg, reg_exp, REG_EXTENDED|REG_NOSUB);
    if (rc != 0)
    {
            if (rc == REG_BADPAT || rc == REG_ECOLLATE)
                    fprintf(stderr, "Bad Regex/Collate\n");
            if (rc == REG_ECTYPE)
                    fprintf(stderr, "Invalid Char\n");
            if (rc == REG_EESCAPE)
                    fprintf(stderr, "Trailing \\\n");
            if (rc == REG_ESUBREG || rc == REG_EBRACK)
                    fprintf(stderr, "Invalid number/[] error\n");
            if (rc == REG_EPAREN || rc == REG_EBRACE)
                    fprintf(stderr, "Paren/Bracket error\n");
            if (rc == REG_BADBR || rc == REG_ERANGE)
                    fprintf(stderr, "{} content invalid/Invalid endpoint\n");
            if (rc == REG_ESPACE)
                    fprintf(stderr, "Memory error\n");
            if (rc == REG_BADRPT)
                    fprintf(stderr, "Invalid regex\n");

            fprintf(stderr, "%s: Failed to compile the regular expression:%d\n", __func__, rc);
            return 1;
    }
    while (status)
    {
            fgets(email, sizeof(email), stdin);
            status = email[0]-48;

            rc = regexec(&preg, email, (size_t)0, NULL, 0);
            if (rc == 0)
            {
                    fprintf(stderr, "%s: The regular expression is a match\n", __func__);
            }
            else
            {
                    fprintf(stderr, "%s: The regular expression is not a match: %d\n", __func__, rc);
            }
    }

    regfree(&preg);

    return 0;
}

正则表达式编译失败并出现以下错误。

The regex = [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Invalid regex
main: Failed to compile the regular expression:13

出现这个错误的原因是什么?正则表达式是否需要修改?

谢谢, 马修·李居

We need to write a email validation program in C. We are planning to use GNU Cregex.h) regular expression.

The regular expression we prepared is

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

But the below code is failing while compiling the regex.

#include <stdio.h>
#include <regex.h>

int main(const char *argv, int argc)
{

    const char *reg_exp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";

int status = 1;

    char email[71];

    regex_t preg;

    int rc;

    printf("The regex = %s\n", reg_exp);

    rc = regcomp(&preg, reg_exp, REG_EXTENDED|REG_NOSUB);
    if (rc != 0)
    {
            if (rc == REG_BADPAT || rc == REG_ECOLLATE)
                    fprintf(stderr, "Bad Regex/Collate\n");
            if (rc == REG_ECTYPE)
                    fprintf(stderr, "Invalid Char\n");
            if (rc == REG_EESCAPE)
                    fprintf(stderr, "Trailing \\\n");
            if (rc == REG_ESUBREG || rc == REG_EBRACK)
                    fprintf(stderr, "Invalid number/[] error\n");
            if (rc == REG_EPAREN || rc == REG_EBRACE)
                    fprintf(stderr, "Paren/Bracket error\n");
            if (rc == REG_BADBR || rc == REG_ERANGE)
                    fprintf(stderr, "{} content invalid/Invalid endpoint\n");
            if (rc == REG_ESPACE)
                    fprintf(stderr, "Memory error\n");
            if (rc == REG_BADRPT)
                    fprintf(stderr, "Invalid regex\n");

            fprintf(stderr, "%s: Failed to compile the regular expression:%d\n", __func__, rc);
            return 1;
    }
    while (status)
    {
            fgets(email, sizeof(email), stdin);
            status = email[0]-48;

            rc = regexec(&preg, email, (size_t)0, NULL, 0);
            if (rc == 0)
            {
                    fprintf(stderr, "%s: The regular expression is a match\n", __func__);
            }
            else
            {
                    fprintf(stderr, "%s: The regular expression is not a match: %d\n", __func__, rc);
            }
    }

    regfree(&preg);

    return 0;
}

The regex compilation is failing with the below error.

The regex = [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Invalid regex
main: Failed to compile the regular expression:13

What is the cause of this error? Whether the regex need to be modified?

Thanks,
Mathew Liju

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎言笑 2024-09-11 02:27:15

如果您有兴趣,

我最近看到了 完美电子邮件正则表达式终于找到 的帖子黑客新闻
这是关于比较电子邮件地址验证正则表达式

正则表达式,

// James Watts and Francisco Jose Martin Moreno are the first to develop one which  
// passes all of the tests.
/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i

// Arluison Guillaume has also improved Warren Gaebel's regex.
// This one will work in JavaScript:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i 

In case you're interested,

I saw recently the Perfect email regex finally found post on Hacker News and
it's about the Comparing E-mail Address Validating Regular Expressions.

The regexs,

// James Watts and Francisco Jose Martin Moreno are the first to develop one which  
// passes all of the tests.
/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i

// Arluison Guillaume has also improved Warren Gaebel's regex.
// This one will work in JavaScript:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i 
时间你老了 2024-09-11 02:27:15

我在标准 C 中使用这个 POSIX 表达式:

const char *reg_exp = "^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*@([a-z0-9])"
                      "(([a-z0-9-])*([a-z0-9]))+(.([a-z0-9])([-a-z0-9_-])?"
                      "([a-z0-9])+)+$";

I use this POSIX expression in standard C:

const char *reg_exp = "^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*@([a-z0-9])"
                      "(([a-z0-9-])*([a-z0-9]))+(.([a-z0-9])([-a-z0-9_-])?"
                      "([a-z0-9])+)+$";
忘东忘西忘不掉你 2024-09-11 02:27:15

您的问题是序列 (? 的四个实例。这是没有意义的 - ( 启动一个新的子正则表达式,而您不能有 ? 在正则表达式的开头。

Your problem is the four instances of the sequence (?. That's meaningless - the ( starts a new sub-regex and you can't have ? at the start of a regex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文