regcmp模式$ 0的含义

时间:2012-05-09 07:49:34

标签: c linux unix

我试图理解regcmp()regex()的工作原理。我的代码是

int main()
{ 
    char *newcursor, *name; char *string; char ret0[9];

    name = regcmp("([A-Za-z][A-za-z0-9]{0,4})$0", (char *)0);
    printf("name %s\n",&(*name));
    newcursor = regex(name, "filter:attrsonly:attrs", ret0);
    printf("newcursor %s  and ret0 %s\n",newcursor,ret0);
    return 0;
}

第12行在模式$0末尾的([A-Za-z][A-za-z0-9]{0,4})$0是什么意思?

我正在使用LINUX中的regex()regcmp()函数替换regexec()regcomp(),以便将代码从UNIX移植到LINUX regcmp()和{{ LINUX中没有1}}。

如果我从模式中删除regex(),则只有在执行$0时才会在LINUX中给出预期结果。 regcomp()是什么意思?

1 个答案:

答案 0 :(得分:1)

让我引用man 7 regex

     '$' (matching the null string at the end of a line),

unix程序使用基本正则表达式的可能性很小:

    Obsolete ("basic") regular expressions differ in several respects.
     [ ... ]
    '$' is  an  ordinary  character except  at the end of the RE or(!)
    the end of a parenthesized subexpression
     [ ... ]

编辑:好的,我应该查一下unix - regcmp,我以为你已经做过了:

   ( ... )$n         The value of the enclosed regular expression is to be
                     returned. The value will be  stored  in  the  (n+1)th
                     argument following the subject argument. At most, ten
                     enclosed regular expressions are allowed. The regex()
                     function makes its assignments unconditionally.

所以在这种情况下,$0只是指定匹配结果的去向,所以你可以把它留下来。