简体   繁体   中英

regular expression for ipv4 address in CIDR notation

I am using the below regular expression to match ipv4 address in CIDR notation.

[ \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]

It seems to match the following example 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? 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.

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

dst-ip { 192.168.10.5/10 }

The " / " in your IPV4ADDRESS pattern needs to be escaped (" \\/ ").

An un-escaped " / " in a flex pattern is the trailing context operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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