2012-05-09 26 views
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与regexec()regcomp()功能替换regex()regcmp()到端口的代码从UNIX到Linux作为regcmp()regex()不存在在LINUX。

如果我从模式中删除$0,它只会在LINUX执行regcomp()时给出预期的结果。 $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. 

so this in this如果$0只是指定比赛结果应该在哪里,那么您可以将其忽略。

+0

我明白'$'(匹配行末尾的空字符串),但我理解了过时(“基本”)等的剩余部分。如果它在LINUX中没有$ 0就可以工作,那么我可以继续? – bhuvana

+0

希望更新的答案更好地解释它。否则请按照手册页的链接。 – mata

+0

非常感谢! – bhuvana