用std::regex_match匹配个位数

Matching single digit with std::regex_match

本文关键字:match std regex      更新时间:2023-10-16

根据这个参考,我应该能够将一个数字与匹配

std::regex e1 ("\d");  

然而,当我运行以下测试代码时,我会得到一个regex异常。

#include <iostream>
#include <string>
#include <regex>
int main()
{
  std::regex r("\d");
  std::string s("9");
  if (std::regex_match(s, r)) { std::cout << "matched!" << std::endl; }
}

GCC的std::regex支持还没有准备好进入黄金时段。请参阅:gcc 4.8或更早版本对正则表达式有缺陷吗?

如果std::regex支持仍然像@qwrrty所建议的那样有缺陷,那么字符类'[0-9]'就是'd'的替代品。