定期EXP验证电子邮件用C [英] Regular exp to validate email in C

查看:99
本文介绍了定期EXP验证电子邮件用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要用C编写电子邮件验证计划,我们计划使用GNU Cregex.h)定期EX pression。

常规前pression我们prepared是

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

不过,在编译正则表达式以下code失败。

 的#include&LT;&stdio.h中GT;
#包括LT&;&regex.h GT;主要INT(为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状态= 1;    字符的电子邮件[71];    regex_t $ P $皮克;    INT RC;    的printf(正则表达式=%S \\ n,reg_exp);    RC = regcomp(安培; $ P $皮克,reg_exp,REG_EXTENDED | REG_NOSUB);
    如果(RC!= 0)
    {
            如果(RC == || REG_BADPAT RC == REG_ECOLLATE)
                    fprintf中(标准错误,错误的正则表达式/自动分页\\ n);
            如果(RC == REG_ECTYPE)
                    fprintf中(标准错误,无效字符\\ n);
            如果(RC == REG_EESCAPE)
                    fprintf中(标准错误,尾随\\\\\\ N);
            如果(RC == || REG_ESUBREG RC == REG_EBRACK)
                    fprintf中(标准错误,无效号码/ []错误。\\ n);
            如果(RC == || REG_EPAREN RC == REG_EBRACE)
                    fprintf中(标准错误,括号/支架错误。\\ n);
            如果(RC == || REG_BADBR RC == REG_ERANGE)
                    fprintf中(标准错误,{}内容无效/端点无效\\ n);
            如果(RC == REG_ESPACE)
                    fprintf中(标准错误,内存错误。\\ n);
            如果(RC == REG_BADRPT)
                    fprintf中(标准错误,无效的正则表达式\\ n);            fprintf中(标准错误,%S:无法编译正前pression数:%d \\ n,__func__,RC);
            返回1;
    }
    而(状态)
    {
            与fgets(电子邮件,的sizeof(电子邮件),标准输入);
            状态=电子邮件[0] -48;            RC = regexec(安培; $ P $皮克,电子邮件(为size_t)0,NULL,0);
            如果(RC == 0)
            {
                    fprintf中(标准错误,%S:正恩pression一个匹配\\ n,__func__);
            }
            其他
            {
                    fprintf中(标准错误,%S:正恩pression不匹配数:%d \\ n,__func__,RC);
            }
    }    regfree(安培; preG);    返回0;
}

正则表达式编译未能与下面的错误。

 正则表达式= [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
正则表达式无效
主:无法编译正前pression: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

解决方案

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.

这篇关于定期EXP验证电子邮件用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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