forest_lzs 2019-04-25 11:31 采纳率: 0%
浏览 936
已结题

使用C的正则表达式,匹配不了\d

使用C的正则表达式,但死活匹配不了\d,但换成\w可以匹配成功。有大神知道是什么原因么?

#include <regex.h>
using namespace std;
int main(int argc, char **argv) {
    string text = "123abc";
    string sExpression = "\\d";
    regex_t reg;
    regcomp(&reg, sExpression.c_str(), REG_EXTENDED | REG_NOSUB);

    regmatch_t pmatch[1];
    int iRet = regexec(&reg, text.c_str(), 1, pmatch, 0);
    if (0 == iRet) {
        cout << "match" << endl;
    }
    else {
        cout << "not match" << endl;
        fprintf(stdout, "ret[%d]\n", iRet);
    }
    regfree(&reg);
    return 0;
}

把上面的\d换成\w则返回成功。原因找到了,可参考
https://stackoverflow.com/questions/37956719/when-using-regex-in-c-d-does-not-work-but-0-9-does
GNU 的 ERE就是不支持\d。。

  • 写回答

1条回答

  • threenewbee 2019-04-25 12:24
    关注
    string sExpression = "\\d+(?=\\w+)";
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊