繁体   English   中英

正则表达式库错误

[英]Regular expression library bug

我使用T-Rex正则表达式库将输入的字符串验证为以下正则表达式

(“(([A-PR-UWYZ][A-HK-Y]{0,1}[0-9]{1,2}[ABCEHMNPRTVWXY]{0,1}|GIR)\s{0,2}([0-9][ABD-HJLN-UW-Z]{2}))”)

但是应传递的字符串如"A11AA"失败。 你可以帮帮我吗 ?

您可以在http://sourceforge.net/projects/tiny-rex/中找到T-Rex源代码。

一个猜测,出乎意料:

#include "trex.h"
#include <stdio.h>
#include <string.h>

#ifdef _UNICODE
#define trex_sprintf swprintf
#else
#define trex_sprintf sprintf
#endif

int main(int argc, char* argv[])
{
    const TRexChar *begin,*end;
    TRexChar sTemp[200];
    const TRexChar *error = NULL;
    TRex *x = trex_compile(_TREXC(

  "("
    "("
      "("
        "[A-P]|[R-U]|[WYZ]"
      ")"
      "("
        "[A-H]|[K-Y]"
      ")?"

      "[0-9]{1,2}"

      "([ABCEHMNPRTVWXY]?|GIR)"
     ")"

     "\\s{0,2}"
     "("
       "[0-9]"
       "("
        "[AB]|[D-H]|[JL]|[N-U]|[W-Z]"
       "){2}"
     ")"
   ")"),&error);

    if(x) {
        trex_sprintf(sTemp,_TREXC("A11AA"));
        if(trex_search(x,sTemp,&begin,&end))
        {
            int i,n = trex_getsubexpcount(x);
            TRexMatch match;
            for(i = 0; i < n; i++)
            {
                TRexChar t[200];
                trex_getsubexp(x,i,&match);
                trex_sprintf(t,_TREXC("[%%d]%%.%ds\n"),match.len);
                trex_printf(t,i,match.begin);
            }
            trex_printf(_TREXC("match! %d sub matches\n"),trex_getsubexpcount(x));
        }
        else {
            trex_printf(_TREXC("no match!\n"));
        }
        trex_free(x);
    }
    else {
        trex_printf(_TREXC("compilation error [%s]!\n"),error?error:_TREXC("undefined"));
    }
    return 0;
}

通常,必须在调用“ Bug!”之前检查三次。 在编译器,库或其他工具上(开发人员大概知道他们在做什么,并测试他们的东西)。 此外,您可以在公开场合表现自己;-)

AFAICS,您的“ A11AA”字符串与给定的表达式不匹配,这之后需要更多的东西。

尝试使用更简单的表达式,匹配字符串的各个部分并进行处理。 检查表达式的确切含义(不同工具的语言非常不同)。

暂无
暂无

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

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