c - regcmp 模式 $0 含义

标签 c linux unix

我正在尝试了解 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/

相关文章:

在c中顺时针打印4X4矩阵

sql - 无法使用 google cloudSQL 连接所需的端口

linux - 文件的文件权限

linux - 遍历文件并用 sed 替换每一行

c - 为什么我的多线程程序有时会阻塞?

c++ - 从 dll 中删除符号

c - 如何避免在控制台上打印来自 syslog 的广播消息

linux - AWK 字段包含数字范围

c - 带有 mmap 的 shm_open 仅在一台特定机器上给出总线错误

linux - POSIX 线程是如何在 Linux 上实现的?