2012-05-09 33 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