这是我用来测试正则表达式的代码:
#include <iostream>
#include <boost/regex.hpp>
//#include <regex>
#include <functional>
#include <string>
int main(int argc, char** argv)
{
auto isMatch = [](const std::string& text, const std::string& pattern)->bool
{
return boost::regex_search(text,
boost::regex(pattern, boost::regex::icase | boost::regex::nosubs)
);
};
std::cout << std::boolalpha << isMatch("Hello, world!",
R"(((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;)))") << std::endl;
return 0;
}
代码编译得很好,但是当我运行它时,有时我会遇到断言失败,因为我可以安全地假设它是一个不应为 null 的 null shared_ptr。它似乎是随机发生的,如下所示:
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$
这就是我安装boost的方式:
./bootstrap.sh --prefix=/usr
sudo ./b2 install
我知道这个库过去一直存在这个问题,但我一直无法找到令人满意的解决方法;我想知道一个是否已经浮出水面,我只是还没有看到它。但是,鉴于错误的片状性质,我不确定我是否也遇到了同样的问题。std::regex
只是在运行时抛出std::regex_error
s 所以我不能使用它。
(我知道 isMatch 在这种情况下不需要是 lambda 甚至是真正的函数,但在我的非人为代码中,这样做是有原因的。)