我想了解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是什么意思?
我在linux中将([A-Za-z][A-za-z0-9]{0,4})$0regex()替换为regcmp()regexec()函数,以便将代码从unix移植到linux,因为regcomp()regcmp()在linux中不存在。
如果我从模式中删除了regex(),它只会在linux中执行$0时给出预期的结果。什么意思?

最佳答案

让我引用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只指定匹配的结果应该放在哪里,所以您可以忽略它。

关于c - regcmp模式$ 0的含义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10511921/

10-11 21:13