【问题标题】:boost regex error: uninitialzed boost::match_resultsboost 正则表达式错误:未初始化的 boost::match_results
【发布时间】:2014-03-04 18:35:33
【问题描述】:

运行以下时

    bool my_compare(const std::string& string_1, const std::string& string_2)
    {
        const boost::regex str_re("so(m)e_(r)e(g)ex");
        boost::smatch str_match;     

        size_t one;
        uint8_t two;
        size_t three;
        if( boost::regex_search(string_1, str_match, str_re) ) {
            one = boost::lexical_cast<size_t>( std::string(str_match[1].first, match[1].second) );
            two = boost::lexical_cast<uint8_t>( std::string(str_match[2].first, match[2].second) );
            three = boost::lexical_cast<size_t>( std::string(str_match[3].first, match[3].second) );
        }
        size_t four;
        uint8_t five;
        size_t six;
        if( boost::regex_search(string_1, str_match, str_re) ) {
            four = boost::lexical_cast<size_t>( std::string(str_match[1].first, match[1].second) );
            five = boost::lexical_cast<uint8_t>( std::string(str_match[2].first, match[2].second) );
            six = boost::lexical_cast<size_t>( std::string(str_match[3].first, match[3].second) );
        }
        return false;
    }

    int main()
    {
       my_compare(some_string1, some_string2);
       return 0;
    }

我收到以下错误,我不明白:

在抛出一个实例后调用终止 'boost::exception_detail::clone_impl

' what(): bad lexical cast: 源类型值不能被解释为目标

【问题讨论】:

  • str_matchmatch 应该是一样的吗?它有助于发布可编译的代码。
  • match[x] 由正则表达式生成。 str_match 是用于结果的对象
  • 显示的代码没有初始化甚至没有声明任何名为match的东西。

标签: c++ regex exception boost


【解决方案1】:

"so(m)e_(r)e(g)ex"

此正则表达式将使用字符串"m" 填充捕获str_match[1],使用字符串"r" 填充str_match[2] 以及使用字符串"g" 填充str_match[3]

 boost::lexical_cast<size_t>( std::string(str_match[1].first, match[1].second) );

这将尝试将字符串"m" 转换为size_t 类型的值,失败(“m”不是整数!),并抛出异常

' what(): bad lexical cast: 源类型值不能被解释为目标

(顺便说一句,你不需要从一对迭代器中用字符串构造函数跳过这些箍,子匹配可以直接转换为字符串)

【讨论】:

  • 正则表达式是一个例子,我没有使用。但是,您帮助我找到了铸造的问题。谢谢!
猜你喜欢
  • 2011-07-24
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2011-07-10
相关资源
最近更新 更多