POSIX 扩展正则表达式是否支持 \d 元字符? [英] Does POSIX Extended Regular Expressions supports the \d Metacharacter?

查看:65
本文介绍了POSIX 扩展正则表达式是否支持 \d 元字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have witten a simple c program as follows:

#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;
}

我已阅读 维基百科 上的 POSIX 正则表达式标准,但没有找到任何证据对于 POSIX 支架是否支持 \d 元字符.

I have read the POSIX regular expressions standard on Wikipedia, I haven't found any evidence for that whether the POSIX stand suports the \d metacharacter.

或者我的代码中有一个细微的错误?

Or there is a subtle mistake in my code?

推荐答案

根据 this,POSIX 正则表达式不支持 \d 速记(也不支持 \w\s).

According to this, POSIX regular expressions do not support the \d shorthand (and neither do they support \w or \s).

这篇关于POSIX 扩展正则表达式是否支持 \d 元字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆