我有两个正则表达式。一个匹配python风格的注释,一个匹配文件路径。
当我尝试查看注释是否与文件路径表达式匹配时,如果注释字符串长于 ~15 个字符,则会引发错误。否则,它会按预期运行。
如何修改我的正则表达式使其不存在此问题
示例代码:
#include <string>
#include "boost/regex.hpp"
using namespace std;
using namespace boost;
int main(int argc, char** argv)
{
boost::regex re_comment("\\s*#[^\\r\\n]*");
boost::regex re_path("\"?([A-Za-z]:)?[\\\\/]?(([^(\\\\/:*?\"<>|\\r\\n)]+[\\\\/]?)+)?\\.[\\w]+\"?");
string shortComment = " #comment ";
string longComment = "#123456789012345678901234567890";
string myPath = "C:/this/is.a/path.doc";
regex_match(shortComment,re_comment); //evaluates to true
regex_match(longComment,re_comment); //evaluates to true
regex_match(myPath, re_path); //evaluates to true
regex_match(shortComment, re_path); //evaluates to false
regex.match(longComment, re_path); //throws error
}
这是抛出的错误
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.