【问题标题】:Simple Regex Usage in C++C++ 中的简单正则表达式用法
【发布时间】: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]+
  • 我只是在尝试所有可能的正则表达式,任何 '*' '+' 字符都会使正则表达式构造函数崩溃..

标签: c++ regex std


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2010-12-05
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多