使用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(®, sExpression.c_str(), REG_EXTENDED | REG_NOSUB);
regmatch_t pmatch[1];
int iRet = regexec(®, text.c_str(), 1, pmatch, 0);
if (0 == iRet) {
cout << "match" << endl;
}
else {
cout << "not match" << endl;
fprintf(stdout, "ret[%d]\n", iRet);
}
regfree(®);
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。。