【问题标题】:Boost ( 1.34) Regex syntax bugBoost(1.34)正则表达式语法错误
【发布时间】:2011-07-24 13:07:21
【问题描述】:

我已经阅读了一些文档,并且我更熟悉 VS2010 附带的当前版本。但是现在我被 ubuntu 8.04 和 boost 1.34 卡住了,并且遇到了一些奇怪的错误。谁能告诉我做错了什么。这是regex_search boost v1.34的手册页

这是我在代码中所做的:

std::string sLine;
getline(dataFile, sLine);
boost::match_results<std::string::const_iterator> lineSmatch; 
boost::match_flag_type regFlags = boost::match_default;    
boost::regex finalRegex(linePattern);

boost::regex_search(sLine.begin(), sLine.end(), lineSmatch, finalRegex, regFlags);

这里是编译错误:

错误:没有匹配函数调用 'regex_search(__gnu_cxx::__normal_iterator, std::allocator >>, __gnu_cxx::__normal_iterator, std::allocator >>, boost::match_results<:__normal_iterator std: :allocator> >, std::allocator, std::allocator > > > > >&, boost::regex&, boost::regex_constants::match_flag_type&)'

【问题讨论】:

    标签: regex boost c++11 boost-regex


    【解决方案1】:

    如果您要将regex_search 应用于sLine 本身而不是 iterator 范围,正如 Howard 回答的那样,您可以使用 sLine 而不是 begin()end().
    例如:

    boost::regex_search(sLine, lineSmatch, finalRegex, regFlags);
    

    如果您必须将iterator 范围指定为regex_search,因为类型 match_results 的参数是 const_iterator,第一个和第二个 regex_search 的参数也必须是 const_iterator
    例如:

    std::string::const_iterator b = sLine.begin(), e = sLine.end();
    boost::regex_search(b, e, lineSmatch, finalRegex, regFlags);
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      无法专门为您提供 ubuntu 8.04 和 boost 1.34 的帮助。但是,以下代码在实现 C++11 的 libc++ 上为我编译。也许它与您的环境足够接近,可以告诉您哪里出了问题。

      #include <regex>
      #include <fstream>
      
      int main()
      {
          std::ifstream dataFile;
          std::string sLine, linePattern;
          getline(dataFile, sLine);
          std::match_results<std::string::const_iterator> lineSmatch; 
          std::regex_constants::match_flag_type regFlags = 
                                              std::regex_constants::match_default;    
          std::regex finalRegex(linePattern);
      
          std::regex_search(sLine, lineSmatch, finalRegex, regFlags);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        • 2019-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多