我正在尝试了解 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
是什么意思?
我正在用 regexec()
和 regcomp()
替换 regex()
和 regcmp()
LINUX 中的函数将代码从 UNIX 移植到 LINUX,因为 regcmp()
和 regex()
在 LINUX 中不存在。
如果我仅从模式中删除 $0
,它会在执行 regcomp()
时在 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/