简体   繁体   English

CIDR表示法中ipv4地址的正则表达式

[英]regular expression for ipv4 address in CIDR notation

I am using the below regular expression to match ipv4 address in CIDR notation. 我正在使用以下正则表达式来匹配CIDR表示法中的ipv4地址。

[ \t]*(((2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)\.){3}(2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)(/(3[012]|[12]?[0-9])))[ \t]*

I have tested the above using [http://regexpal.com/][1] 我已经使用[http://regexpal.com/][1]测试了以上内容

It seems to match the following example 192.168.5.10/24 似乎符合以下示例192.168.5.10/24

However when I use the same example in flex it says "unrecognized rule".Is there some limitation in flex in that it does not support all the features? 但是,当我在flex中使用相同的示例时,它会显示“无法识别的规则”。flex是否存在一些局限性,因为它不支持所有功能? The above regex seems pretty basic without the use of any extended features.Can some one point out why flex is not recognizing the rule. 上面的正则表达式似乎很基本,没有使用任何扩展功能。有人可以指出为什么flex无法识别规则。

Here is a short self contained example that demonstrates the problem 这是一个简短的自包含示例,演示了该问题

IPV4ADDRESS [ \t]*(((2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)\.){3}(2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)(/(3[012]|[12]?[0-9])))[ \t]*
SPACE [ \t]

%x S_rule S_dst_ip

%%

%{
    BEGIN S_rule;
%}

<S_rule>(dst-ip){SPACE}   {
           BEGIN(S_dst_ip);
        }

<S_dst_ip>\{{IPV4ADDRESS}\}  {
       printf("\n\nMATCH [%s]\n\n", yytext);
       BEGIN S_rule;
     }

. { ECHO; }

%%

int main(void)
{
    while (yylex() != 0)
        ;
    return(0);
}

int yywrap(void)
{
    return 1;
}

When I try to do flex test.l it give "unrecognized rule" error.I want to match 当我尝试执行flex test.l时,它给出“无法识别的规则”错误。我要匹配

dst-ip { 192.168.10.5/10 }

The " / " in your IPV4ADDRESS pattern needs to be escaped (" \\/ "). IPV4ADDRESS模式中的“ / ”需要转义(“ \\/ ”)。

An un-escaped " / " in a flex pattern is the trailing context operator. flex模式中未转义的“ / ”是尾随上下文运算符。

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

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