【问题标题】:Does POSIX Extended Regular Expressions supports the \d Metacharacter?POSIX 扩展正则表达式是否支持 \d 元字符?
【发布时间】:2011-11-25 11:02:58
【问题描述】:

我写了一个简单的c程序如下:

#include <stdio.h>
#include <regex.h>

int main(int argc, char* argv[])
{
    regex_t re;
    char *pattern = "\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1"; // use \d, but wouldn't match
    const char *target = "123Hello N/A Hello";

    regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB);

    int ret = regexec(&re, target, (size_t) 0, NULL, 0);
    if(ret == REG_NOMATCH) {
        printf("\n%s\n does not match \n%s\n", target, pattern);
    }

    pattern = "[0-9]{3}([a-zA-Z]+).([0-9]{2}|N/A)\\s\\1"; // changes to [0-9], now OK

    regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB);

    ret = regexec(&re, target, (size_t) 0, NULL, 0);
    if(ret == REG_NOMATCH) {
        printf("\n%s\n does not match \n%s\n", target, pattern);
    }

    regfree(&re);
    return 0;
}

我已经阅读了Wikipedia 上的 POSIX 正则表达式标准,我还没有找到任何证据证明 POSIX 立场是否支持 \d 元字符。

还是我的代码有细微的错误?

【问题讨论】:

    标签: regex


    【解决方案1】:

    根据this,POSIX 正则表达式不支持\d 简写(也不支持\w\s)。

    【讨论】:

    • 谢谢,这篇文章消除了我的困惑。我希望得到每个标准的准确特性列表,有这样的东西吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2021-09-07
    相关资源
    最近更新 更多