c++ std::regex 与预期不匹配

标签 c++ regex

我尝试在 MS VC++ 2012 中使用 c++ std::regex 实现一个简单的字符串测试方法。

const std::string str = "1.0.0.0029.443";

if ( std::regex_match( str, std::regex( "\\.0\\d+" ) ) )
    std::cout << "matched." << std::endl;

我猜代码会匹配给定字符串的“.0029”部分。但是,它根本不匹配。 我在 http://regex101.com/ 上试过了它奏效了。

最佳答案

使用std::regex_search而不是返回您的子匹配项。

const std::string str = "1.0.0.0029.443";

std::regex rgx("(\\.0[0-9]+)");
std::smatch match;

if (std::regex_search(str.begin(), str.end(), match, rgx)) {
    std::cout << match[1] << '\n';
}

关于c++ std::regex 与预期不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319103/

相关文章:

c++ - 无法从文件 C++ 中读取 double 行

javascript - Js正则表达式测试除字符串

JavaScript RegEx 拆分数学运算符,同时将它们保留为单独的项目

regex - 使用另一个 get params 重写查询字符串

c++ - 使用 TinyXML 解析 XML 文件 C++

c++ - atomic() 中的++、add 操作和 fetch_add() 有什么区别

c++ - 在单例类中声明和访问枚举 - C++

c++ - 有效地矢量化图像 block 处理?

javascript - 如果字符串中存在数组值(类似于正则表达式)

regex - 在 perl 中解释正则表达式