如何使用std :: regex?

Yue Wang

我正在使用C ++ Primer 5th学习C ++。以下代码是从P729复制而来的。

#include <iostream>
#include <regex>
#include <string>

int main()
{
    // find the characters ei that follow a character other than c
    std::string pattern("[^c]ei");
    pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
    std::regex r(pattern);
//  ~~~~~~~~~~~^~~~~~~~~~~ where the exception was thrown.
    std::smatch results;

    std::string test_str = "receipt freind theif receive";
    if (regex_search(test_str, results, r))
        std::cout << results.str() << std::endl;

    return 0;
}

运行它时,抛出了一个异常:

terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error
Press <RETURN> to close this window...

通过逐步调试,我发现它是r从的代码构造对象时抛出的bits/regex_compiler.h

  template<typename _InIter, typename _TraitsT>
    bool
    _Compiler<_InIter, _TraitsT>::
    _M_bracket_expression()
    {
      if (_M_match_token(_ScannerT::_S_token_bracket_begin))
    {
      _RMatcherT __matcher(_M_match_token(_ScannerT::_S_token_line_begin),
                   _M_traits);
      if (!_M_bracket_list(__matcher)
          || !_M_match_token(_ScannerT::_S_token_bracket_end))

        __throw_regex_error(regex_constants::error_brack);
     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      _M_stack.push(_StateSeq(_M_state_store,
                  _M_state_store._M_insert_matcher(__matcher)));
      return true;
    }
      return false;
    }

好的,上面的代码完全超出了我的理解,这是我到目前为止已经尝试过的,谁能告诉我这里的锣声是什么?如何解决?

更新:我正在使用的编译器:

gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~13.04)
雅库布·巴斯基·加布佐(Jakub BaskiGabčo)

您需要使用最低版本的g ++ 4.9才能正确使用正则表达式。(可以使用较低版本进行编译,但已损坏)

您可以从他们那边下载g ++ 4.9。GCC站点
之后,尝试:
g ++ 49 -std = c ++ 0x -static-libstdc ++。

我会尝试为您搜索更多有关它的信息。

编辑:g ++ 49,由于这个原因,版本4.9在构建之后不能被默认。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用std :: regex匹配多个结果

来自分类Dev

如何使用std :: regex匹配多个结果

来自分类Dev

C ++。如何使用std :: regex替换括号内的任何内容?

来自分类Dev

使用std :: regex过滤输入

来自分类Dev

如何知道libstdc ++是否支持std :: regex

来自分类Dev

使用 std::regex 按点解析数字

来自分类Dev

在C ++ STL中,如何使用regex_replace从std :: string中删除非数字字符?

来自分类Dev

如何在cpp中使用std :: regex_replace()函数查找和替换

来自分类Dev

如何使用 std::regex_replace 将字符串替换为小写?

来自分类Dev

bug in std :: regex?

来自分类Dev

如何使用regex_replace?

来自分类Dev

如何使用Regex取代字串

来自分类Dev

返回std.regex.regex的值?

来自分类Dev

std :: regex忽略regex命令中的空格

来自分类Dev

如何使用std :: regex查找字符串中的下一个匹配项?

来自分类Dev

C ++ std :: regex如何修复error_complexity?

来自分类Dev

C ++使用std :: regex逐行拆分字符串

来自分类Dev

gsl :: span无法使用std :: regex进行编译

来自分类Dev

使用C ++ 11的std :: regex麻烦填充子匹配

来自分类Dev

C ++ 11 std :: regex替代

来自分类Dev

std :: regex_match与字符éèà

来自分类Dev

std :: regex构造抛出异常

来自分类Dev

使用\ d的RegEx在使用[[

来自分类Dev

C#,如何使用Regex验证JSON

来自分类Dev

如何使用RegEx删除可能嵌套的括号?

来自分类Dev

如何使用RegEx选择最长的比赛?

来自分类Dev

如何找到匹配项并使用RegEx更新?

来自分类Dev

如何使用RegEx精确匹配或列出

来自分类Dev

如何在if语句中使用REGEX