RegEx表达式将匹配特定格式的字符串(RegEx expression that will match string of a particular format)

编程入门 行业动态 更新时间:2024-10-28 17:23:27
RegEx表达式将匹配特定格式的字符串(RegEx expression that will match string of a particular format)

我有一行ORDER BY DESC(?year) ,其中?year可以是任何后跟问号(?)名称,例如?名称,?地址等。我尝试了ORDER BY DESC\(\?[az]+\)将整个字符串捕获为ORDER BY DESC(?year)但不起作用。

lex文件:

%{ #include <cstdio> #include <iostream> #include "grammar.tab.h" %} %option case-insensitive %% [1-9][0-9]* { yylval.i = atoi(yytext); return INT_NUM; } "ORDER BY DESC\(\?[a-z]+\)" { yylval.s = strdup(yytext); return ORDER_BY_DESC; } "\n" { yylineno++; } \. { return DOT; } [ \t]+ { /* ignore white space */ } "#".* { /* ignore comments */ } [ \t\v\f\r]+ { } . { std::cerr << "Lexical Error!\n"; } %% int yywrap() { return 1; }

野牛档案:

%{ #include <iostream> #include <cstdlib> #include <stdio.h> #include <stdlib.h> #include <sstream> #include <string> #include <regex> using namespace std; extern int yylineno; extern int yylex(); void yyerror(char *s, ...); %} %error-verbose %union { int i; char *s; } %token<i> INT_NUM; %token<s> ORDER_BY_DESC; %% order_by: | ORDER_BY_DESC { string s($<s>1); string str = s.substr(0, s.size() - 1); char *x = new char[str.length() + 1]; order_by_stmt = str; strcpy(x, str.c_str()); $<s>$ = x; } ; %% void yyerror(char *s, ...) { va_list ap; va_start(ap, s); fprintf(stderr, "%d: error: ", yylineno); vfprintf(stderr, s, ap); fprintf(stderr, "\n"); } int main() { yyparse(); return 0; }

I have a line with ORDER BY DESC(?year) where ?year can be anything followed by a question mark (?) such as ?name, ?address etc. I have tried ORDER BY DESC\(\?[a-z]+\) to capture the whole string as ORDER BY DESC(?year) but not working.

lex file:

%{ #include <cstdio> #include <iostream> #include "grammar.tab.h" %} %option case-insensitive %% [1-9][0-9]* { yylval.i = atoi(yytext); return INT_NUM; } "ORDER BY DESC\(\?[a-z]+\)" { yylval.s = strdup(yytext); return ORDER_BY_DESC; } "\n" { yylineno++; } \. { return DOT; } [ \t]+ { /* ignore white space */ } "#".* { /* ignore comments */ } [ \t\v\f\r]+ { } . { std::cerr << "Lexical Error!\n"; } %% int yywrap() { return 1; }

bison file:

%{ #include <iostream> #include <cstdlib> #include <stdio.h> #include <stdlib.h> #include <sstream> #include <string> #include <regex> using namespace std; extern int yylineno; extern int yylex(); void yyerror(char *s, ...); %} %error-verbose %union { int i; char *s; } %token<i> INT_NUM; %token<s> ORDER_BY_DESC; %% order_by: | ORDER_BY_DESC { string s($<s>1); string str = s.substr(0, s.size() - 1); char *x = new char[str.length() + 1]; order_by_stmt = str; strcpy(x, str.c_str()); $<s>$ = x; } ; %% void yyerror(char *s, ...) { va_list ap; va_start(ap, s); fprintf(stderr, "%d: error: ", yylineno); vfprintf(stderr, s, ap); fprintf(stderr, "\n"); } int main() { yyparse(); return 0; }

最满意答案

"ORDER BY DESC"([^)]*) could be a solution for this question. "ORDER BY DESC"([^)]*) could be a solution for this question.

更多推荐

本文发布于:2023-08-03 16:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   字符串   格式   RegEx   format

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!