Boost regex_replace 异常:“...抛出此异常是为了防止“永恒”匹配...”有时被抛出
我正在使用 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."
有趣/烦人的是,在具有相同测试数据的测试程序中似乎不会发生这种情况。关于为什么会发生这种情况和/或如何解决它有什么想法吗?
I am using Boost.Regex(boost-1.42) to remove the first line of a multi-line string(a fairly large string containing multiple lines ending in '\n').
i.e. using regex_replace to do something akin to 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);
}
This code is throwing the following exception:
"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."
Interestingly/annoyingly, this doesn't seem to happen in test programs with the same test data. Any thoughts on why this could be happening and/or how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在正则表达式的开头放置一个“字符串开头”标记(默认 Perl 兼容模式中的“\A”),以使其更明确地表明您希望它仅匹配第一行。
如果没有明确地将开头与字符串匹配,看起来 boost 正在应用其“最左边最长”规则,这就是导致此问题的原因: http://www.boost.org/doc/libs/1_45_0/libs/regex/doc/html/boost_regex/syntax/leftmost_longest_rule。 html
Try putting a "beginning of string" marker ("\A" in the default Perl-compatible mode) at the beginning of the regex, to make it more explicit that you want it to match just the first line.
Without explicitly matching the beginning to the string, it looks like boost is applying its "leftmost longest" rule and that's what's causing this: http://www.boost.org/doc/libs/1_45_0/libs/regex/doc/html/boost_regex/syntax/leftmost_longest_rule.html