【问题标题】:RegEx expression that will match string of a particular format将匹配特定格式的字符串的正则表达式
【发布时间】:2017-04-25 21:49:28
【问题描述】:

我与ORDER BY DESC(?year) 有一行,其中?year 可以是任何后跟问号(?) 例如?名称、?地址等。我尝试ORDER BY DESC\(\?[a-z]+\) 将整个字符串捕获为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;
}

【问题讨论】:

  • 你用的是什么工具? RegExp 的某些实现存在差异。
  • 我正在为 Flex 使用正则表达式。
  • 动作脚本?给我看代码。
  • 在 lex 文件中 ' "ORDER BY DESC(\?[a-z]+)" { yylval.s = strdup(yytext);返回 ORDER_BY_DESC; }' 和野牛文件'order_by: | ORDER_BY_DESC { 字符串 s($1);字符串 str = s.substr(0, s.size() - 1); char *x = new char[str.length() + 1]; strcpy(x, str.c_str()); $$ = x; }'
  • Err,你能把代码放在问题里吗?

标签: regex flex-lexer


【解决方案1】:
"ORDER BY DESC"([^)]*) could be a solution for this question.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多