2

我试图了解如何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中的 and 和regex()函数regcmp()来将代码从 UNIX 移植到 LINUX,因为 LINUX中没有。regexec()regcomp()regcmp()regex()

如果我$0从模式中删除,它只会在 LINUX 中给出预期的结果regcomp()。是什么$0意思?

4

1 回答 1

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.

所以在这种情况下,$0just 指定了匹配结果应该去哪里,所以你可以忽略它。

于 2012-05-09T08:35:06.963 回答