boost正则获取连接问题

我看你有戏 2009-01-19 01:40:40

我想获取下面的连接名称,和连接地址,熟悉boost的朋友请教一下,测试通过,给满分

void main()
{
std::string testString = "sdfs<a href=\"http:/www.baidu.com/\">aacc</a>df192dfsd<a href=\"http:/www.gjw123.com/\">bbdd</a>.168sdfs.sdfs4dsf.sfdsd1asfscvasdf";
cmatch what;
regex expression( "(?=<a\\s+href=\"(.+)\">(.+)</a>)");
if(regex_match(testString,what,expression))
{
int sun = what.size();
cout<<sun<<endl;

for(int i=0;i <what.size();i++);
cout << "str: " <<what.str() <<endl;
}
else
{
cout << "Error "<<endl;
}

}

...全文
118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
larrypeng 2009-01-22
  • 打赏
  • 举报
回复
boost::regex_match()是配对,不能满足你的要求,要用regex_search()
一下是我的程序片段:
void CMyRTFDoc::DisplayContent(CString strContent, CRTF18 *rtf)
{
std::string stdInput((LPCTSTR)strContent);
std::istringstream is(stdInput);
std::string buff;
std::string line;
std::string style;
boost::regex expressionParagraph ("<p(.*)>(.*)</p>");
boost::regex expressionTableRowStart ("<tablerow(.*)>");
boost::regex expressionTableRowEnd ("</tablerow>");
boost::regex expressionTableCellStart ("<tablecell(.*)>");
boost::regex expressionTableCellEnd ("</tablecell>");
boost::regex expressionInput ("<i length=(\\d+);(.*)></i>");
boost::regex expressionComment ("^;(.*)");
boost::regex expressionImage ("<image ID=(.*);WIDTH=(\\d+);HEIGHT=(\\d+);></image>");

boost::smatch what;
std::string::const_iterator begin;
std::string::const_iterator end;
int nLine = 0;

while(std::getline(is, buff))
{
nLine ++;
if(buff.empty())
continue;

begin = buff.begin();
end = buff.end();
if(boost::regex_search(begin, end, what, expressionComment))
{
continue;
}
else if(boost::regex_search(begin, end, what, expressionParagraph))
{
style = std::string(what[1].first, what[1].second);
line = std::string(what[2].first, what[2].second);
OutputParagraph(NORMAL_PARAGRAPH,
nLine,
(char *)line.c_str(),
(char*)style.c_str(),
rtf);
}
else if(boost::regex_search(begin, end, what, expressionTableRowStart))
{
style = std::string(what[1].first, what[1].second);
OutputTableRowStart((char *)style.c_str(), rtf);
}
else if(boost::regex_search(begin, end, what, expressionTableRowEnd))
{
OutputTableRowEnd(rtf);
}
else if(boost::regex_search(begin, end, what, expressionTableCellStart))
{
style = std::string(what[1].first, what[1].second);
if(style.empty())
{
OutputTableCellStart(NULL, rtf);
}
else
{
OutputTableCellStart((char *)style.c_str(), rtf);
}
}
else if(boost::regex_search(begin, end, what, expressionTableCellEnd))
{
OutputTableCellEnd(rtf);
}
else if(boost::regex_search(begin, end, what, expressionInput))
{
std::string strLen = std::string(what[1].first, what[1].second);
std::string strModify = std::string(what[2].first, what[2].second);
int len = atol(strLen.c_str());
OutputInput(nLine, len, (char *)strModify.c_str(), rtf);
}
else if(boost::regex_search(begin, end, what, expressionImage))
{
std::string stdID = std::string(what[1].first, what[1].second);
std::string stdWidth = std::string(what[2].first, what[2].second);
std::string stdHeight = std::string(what[3].first, what[3].second);

CString strID(stdID.c_str());
int width = atol(stdWidth.c_str());
int height = atol(stdHeight.c_str());

if(strID.Compare("image1.gif") == 0)
{
OutputImage(1, width, height, rtf);
}
else if(strID.Compare("image2.gif") == 0)
{
OutputImage(2, width, height, rtf);
}
else if(strID.Compare("image3.gif") == 0)
{
OutputImage(3, width, height, rtf);
}
else if(strID.Compare("image4.gif") == 0)
{
OutputImage(4, width, height, rtf);
}
}

}
return;
}
我看你有戏 2009-01-19
  • 打赏
  • 举报
回复
能具体一点吗高人
taodm 2009-01-19
  • 打赏
  • 举报
回复
.+?

64,648

社区成员

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

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