#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace boost;
static const regex regexp(
"std::vector<"
"(std::map<"
"(std::pair<((\\w+)(::)?)+, (\\w+)>,?)+"
">,?)+"
">");
std::string errorMsg =
"std::vector<"
"std::map<"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>"
">,"
"std::map<"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>"
">"
">";
int main()
{
smatch result;
if(regex_match(errorMsg, result, regexp))
{
for (unsigned i = 0; i < result.size(); ++i)
{
std::cout << result[i] << std::endl;
}
}
// std::cout << errorMsg << std::endl;
return 0;
}
this produces:
這產生:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error>
>' what(): Ran out of stack space trying to match the regular expression.
compiled with
編譯和
g++ regex.cc -lboost_regex
EDIT
編輯
my platform:
我的平台:
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
libboost-regex1.42
Intel(R) Core(TM)2 Quad CPU Q9400 @ 2.66GHz
So the latest Ubuntu 64 bit
13
((\\w+)(::)?)+
is one of the so called "pathological" regular expressions -- it's going to take exponential time, because you have two expressions which are dependent upon each other one right after the other. That is, it fails due to catastrophic backtracking.
(\ w+)(:) +是一種所謂的“病態”正則表達式——它需要指數級的時間,因為有兩個表達式相互依賴,一個接着一個。也就是說,由於災難性的回溯,它失敗了。
Consider if we follow the example of the link, and reduce "something more complicated" to "x". Let's do that with \\w
:
考慮一下我們是否遵循鏈接的示例,將“更復雜的東西”簡化為“x”。讓我們用\\w:
((x+)(::)?)+
Let's also assume that our input is never going to have ::
. Having this actually makes the regex more complex, so if we throw out complexity then we really should be making things simpler if nothing else:
我們還假設我們的輸入永遠不會有::。這樣做實際上會使regex變得更復雜,所以如果我們拋開復雜性,那么我們真的應該讓事情變得更簡單,如果沒有其他的東西:
(x+)+
Now you've got a textbook nested quantifier problem like that detailed in the link above.
現在你有了一個教科書嵌套量詞問題,就像上面鏈接中詳細描述的那樣。
There are a few ways to fix this but the simplest way is probably to just disallow backtracking on the inner match using the atomic group modifier "(?>
":
有幾種方法可以解決這個問題,但最簡單的方法可能是使用原子組修飾符“(?>)”來禁止對內部匹配進行回溯:
((?>\\w+)(::)?)+
1
Tested it locally and it worked fine, my guess is your compiler is doing something weird.
我猜你的編譯器在做一些奇怪的事情。
What version of gcc? what platform? which version of boost?
什么版本的gcc ?什么平台?哪個版本的提高?
-> ./regex
std::vector<std::map<std::pair<Test::Test, int>,std::pair<Test::Test, int>,std::pair<Test::Test, int>>,std::map<std::pair<Test::Test, int>,std::pair<Test::Test, int>,std::pair<Test::Test, int>>>
std::map<std::pair<Test::Test, int>,std::pair<Test::Test, int>,std::pair<Test::Test, int>>
std::pair<Test::Test, int>
Test
Test
::
int
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/12/21/71250b7cf013bdf1baf4208fbb4669aa.html。