【发布时间】:2016-05-16 10:32:36
【问题描述】:
我尝试使用默认库在 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";
}
我尝试了多种语法和标志,但似乎没有任何效果。由于我的代码似乎与我能找到的所有示例相匹配,因此我很难找到我所缺少的。
【问题讨论】:
-
嗯,我应该如何在 c++ 中实际使用正则表达式?
-
@Dillinur gcc 现在最高到 6.1。您可以尝试升级您的编译器。
-
@Dillinur:至少不要使用匹配空字符串的正则表达式模式。为什么需要一个空(null)字符串?只需使用
[0-9]+。 -
我只是在尝试所有可能的正则表达式,任何 '*' '+' 字符都会使正则表达式构造函数崩溃..