gpt4 book ai didi

c++ - C++ 中的简单正则表达式用法

转载 作者:行者123 更新时间:2023-11-30 05:27:31 26 4
gpt4 key购买 nike

我尝试使用默认库在 C++ 中执行最基本的正则表达式示例,但我总是遇到崩溃或不连贯的行为。

// with -std=c++11
#include <regex>
using namespace std;

int main()
{
// Copied from the documentation, this one works
if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
std::cout << "string matched\n";
// The most simple example I could try, crash with "regex_error"
if (std::regex_match ("99", std::regex("[0-9]*") ))
std::cout << "integer matched\n";
}

我尝试了多种语法和标志,但似乎没有任何效果。由于我的代码似乎与我能找到的所有示例都匹配,所以我很难找出我遗漏了什么。

最佳答案

正如@Wiktor Stribiżew 所说,这只是我的编译器太旧了。更新编译器(从 gcc 4.1 到 gcc 4.9)解决了这个问题!

关于c++ - C++ 中的简单正则表达式用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com