2

Boost.Regex(boost-1.42) を使用して、複数行の文字列 ('\n' で終わる複数の行を含むかなり大きな文字列) の最初の行を削除しています。

つまり、regex_replace を使用して s/(.*?) に似た処理を行います\n//

  string
  foo::erase_first_line(const std::string & input) const
  {
    static const regex line_expression("(.*?)\n");
    string  empty_string;

    return boost::regex_replace(input,
                                line_expression,
                                empty_string,
                                boost::format_first_only);
  }

このコードは、次の例外をスローしています。

"terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error> >'
  what():  The complexity of matching the regular expression exceeded predefined bounds.  Try refactoring the regular expression to make each choice made by the state machine unambiguous.  This exception is thrown to prevent "eternal" matches that take an indefinite period time to locate."

興味深い/厄介なことに、これは同じテスト データを使用したテスト プログラムでは発生しないようです。なぜこれが起こっているのか、および/またはそれを修正する方法について何か考えはありますか?

4

1 に答える 1

2

正規表現の先頭に「文字列の先頭」マーカー (デフォルトの Perl 互換モードでは「\A」) を配置して、最初の行だけに一致させたいことをより明確にします。

文字列の先頭を明示的に一致させないと、ブーストが「左端で最も長い」ルールを適用しているように見え、それが原因です: http://www.boost.org/doc/libs/1_45_0/libs/regex/doc/html /boost_regex/syntax/leftmost_longest_rule.html

于 2011-02-11T18:27:53.483 に答える