flex 关于不良品格的正则表达式问题

发布于 2025-01-09 02:42:06 字数 1006 浏览 0 评论 0原文

我是 Flex 正则表达式的新手,我不明白为什么它一直表现出不好的性格。我想知道是否“?!”存在于flex正则表达式中。如果不是,如何更换为正确的?

这是我的代码

%option noyywrap
%{
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

%}

/* ------------------- Rules space -------------------- */
%%

^aa(a|b)*$                    {printf("Token1:%s\n" ,yytext);}
a{3}(a|b)*$             {printf("Token2:%s\n" ,yytext);}
a{2}(a|b)*$             {printf("Token3:%s\n" ,yytext);}
^(?:(a|b){2})+$         {printf("Token4:%s\n" ,yytext);}
^b*(ab*ab*)*$           {printf("Token5:%s\n" ,yytext);}
^(?:.{5})*$             {printf("Token6:%s\n" ,yytext);}
\b((?!aaa)\w)+\b        {printf("Token7:%s\n" ,yytext);} // line 19 error appear here


.|\n        {} // default rule (always include to match all other strings)


%%

/* ----------------- User code space ------------------ */

main()
{
    yylex();
    return;
}

错误

I am new to the flex regular expression and I don't why it keep showing the bad character. I am wondering if the "?!" is exist in flex regular expression. If not, how to replace it with correct one?

Here is my code

%option noyywrap
%{
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

%}

/* ------------------- Rules space -------------------- */
%%

^aa(a|b)*$                    {printf("Token1:%s\n" ,yytext);}
a{3}(a|b)*$             {printf("Token2:%s\n" ,yytext);}
a{2}(a|b)*$             {printf("Token3:%s\n" ,yytext);}
^(?:(a|b){2})+$         {printf("Token4:%s\n" ,yytext);}
^b*(ab*ab*)*$           {printf("Token5:%s\n" ,yytext);}
^(?:.{5})*$             {printf("Token6:%s\n" ,yytext);}
\b((?!aaa)\w)+\b        {printf("Token7:%s\n" ,yytext);} // line 19 error appear here


.|\n        {} // default rule (always include to match all other strings)


%%

/* ----------------- User code space ------------------ */

main()
{
    yylex();
    return;
}

Error

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文