boost::regx error 请大虾们赐教

littleeagle007 2010-02-24 10:42:58
使用boost库: boost_1_41_0

编译环境WIN32 VS2005


出错:

runtime error: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::regex_error> >

搞了好久没有好办法搞的定,请各位大虾赐教一二,拜谢先。

代码
#include <boost/regex.hpp>
#include <iostream>
boost::regex e("(\\d{3, 4})[- ]?(\\d{4)})[- ]?(\\d{4})[- ]?(\\d{4})");
void print_captures(const std::string& regx, const std::string& text)
{
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if (boost::regex_match(text, what, e, boost::match_extra))
{
unsigned i, j;
std::cout << "** Match found **\n Sub-Expressions: \n";
for (i = 0; i < what.size(); ++i)
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
std::cout << " Captures:\n";
for (i = 0; i < what.size(); ++i)
{
std::cout << " $" << i << " = {";
for (j = 0; j < what.captures(i).size(); ++j)
{
if (j)
std::cout << ", ";
else
std::cout << " ";
std::cout << "\"" << what.captures(i)[j] << "\"";
}
std::cout << "}\n";
}
}
else
{
std::cout << "** No match found **\n";
}
}
int main(int , char* [])
{
print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDeeeeeee");
return 0;
}
...全文
138 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
littleeagle007 2012-01-09
  • 打赏
  • 举报
回复
问题搞定, boost 库编译本身存在问题。
赵4老师 2010-02-25
  • 打赏
  • 举报
回复
引用 3 楼 taodm 的回复:
楼主要学会用regex_test这样的工具先验证正则后再使用。

正解
taodm 2010-02-25
  • 打赏
  • 举报
回复
楼主要学会用regex_test这样的工具先验证正则后再使用。
baihacker 2010-02-24
  • 打赏
  • 举报
回复
#include <boost/regex.hpp> 
#include <iostream>

void print_captures(const std::string& regx, const std::string& text)
{ //boost::regex e("(\\d{3, 4})[- ]?(\\d{4)})[- ]?(\\d{4})[- ]?(\\d{4})");
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if (boost::regex_match(text, what, e, boost::match_extra))
{
unsigned i, j;
std::cout << "** Match found **\n Sub-Expressions: \n";
for (i = 0; i < what.size(); ++i)
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
std::cout << " Captures:\n";
}
else
{
std::cout << "** No match found **\n";
}
}
int main(int , char* [])
{
print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDeeeeeee");
return 0;
}

Expression: "(([[:lower:]]+)|([[:upper:]]+))+"
Text: "aBBcccDDDDeeeeeee"
** Match found **
Sub-Expressions:
$0 = "aBBcccDDDDeeeeeee"
$1 = "eeeeeee"
$2 = "eeeeeee"
$3 = "DDDD"
Captures:
Press any key to continue
baihacker 2010-02-24
  • 打赏
  • 举报
回复
boost::regex e("(\\d{3, 4})[- ]?(\\d{4)})[- ]?(\\d{4})[- ]?(\\d{4})");
//你的这个正则式有问题:括号不匹配
//所以在构造这个e的时候异常

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧