简体   繁体   English

正则表达式库错误

[英]Regular expression library bug

I used T-Rex regular expression library to validate an entered string to the following regular expression 我使用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}))”)

But a string like "A11AA" that should be passed fails. 但是应传递的字符串如"A11AA"失败。 Could you help me ? 你可以帮帮我吗 ?

You can find T-Rex source code in http://sourceforge.net/projects/tiny-rex/ . 您可以在http://sourceforge.net/projects/tiny-rex/中找到T-Rex源代码。

A guess, right out of the blue: 一个猜测,出乎意料:

#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;
}

As a rule, you have to check thrice before calling "Bug!" 通常,必须在调用“ Bug!”之前检查三次。 on the compiler, library or other tools (the developers presumably know what they are doing, and test their stuff). 在编译器,库或其他工具上(开发人员大概知道他们在做什么,并测试他们的东西)。 Besides, you could embarass yourself in public ;-) 此外,您可以在公开场合表现自己;-)

AFAICS, your "A11AA" string just doesn't match the given expression, which expects some further stuff after that. AFAICS,您的“ A11AA”字符串与给定的表达式不匹配,这之后需要更多的东西。

Try simpler expressions, matching parts of your string and working up. 尝试使用更简单的表达式,匹配字符串的各个部分并进行处理。 Check the exact meaning of the expressions (the languages of different tools are infuriatingly different). 检查表达式的确切含义(不同工具的语言非常不同)。

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

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