[boost_regex]无法匹配

YaYiYaO 2007-06-30 08:38:03
boost::regex re("<[^>]>");
if (boost::regex_match("<html><body>dddd</body></html>", re))
{
int i = 1;
i++;
i--;
}

很简单的一个式子,我在GRETA中可以匹配,但是怎么用boost就无法匹配呢
还是说boost的语法不是这样的,我看boost regex的文档看的都快头晕了
各位帮我分析下
...全文
295 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
YaYiYaO 2007-07-05
  • 打赏
  • 举报
回复
解决了,还是得看文档啊,呵呵

ps:Greta和boost regex不是一个档次的,Greta什么东西嘛,多用几个断言和".*?"就死那里了,相同的正则,boost regex用了几十毫秒,Greta用了几十分钟都没出来,哎....不说了
taodm 2007-07-02
  • 打赏
  • 举报
回复
再多看看boost::regex的文档吧。
taodm 2007-07-02
  • 打赏
  • 举报
回复
用regex_iterator或regex_token_iterator
YaYiYaO 2007-07-02
  • 打赏
  • 举报
回复
<[^>]*>不就好了吗,干嘛还用什么非贪婪模式
不过我不是来问正则的,我是问boost,在boost中匹配全部,是不是只能一直循环调用regex_search()
正在研究regex boost文档
jixingzhong 2007-07-01
  • 打赏
  • 举报
回复
【Ref】

?, +和*的非贪婪匹配版本,它们尽可能匹配较少的字符;而?, +和*则是贪婪版本,尽可能匹配较多的字符。例如:输入"<abc><def>", 则<.*?> 匹配"<abc>",而<.*>匹配"<abc><def>"。
jixingzhong 2007-07-01
  • 打赏
  • 举报
回复
使用 非贪婪匹配, 在+后面使用 ? 即可
believefym 2007-07-01
  • 打赏
  • 举报
回复
std::string re( "<.+?>" ); 非贪婪匹配
believefym 2007-07-01
  • 打赏
  • 举报
回复
#include <boost/regex.hpp>
#include <iostream>

void parse(boost::regex& expression, const std::string& s)
{
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;

std::string::const_iterator start = s.begin();
std::string::const_iterator end = s.end();
while(boost::regex_search(start, end, what, expression, flags))
{
std::cout << std::string(what[0].first, what[1].second) << "\t";
start = what[0].second;
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
}
std::cout << "\n";
}

int main(int argc, char** argv)
{
std::string re( "<.+?>" );
std::string s1("<html><body>dddd</body></html>");
boost::regex expression(re,boost::regex::icase);

parse(expression, s1);

system("pause");
return 0;
}
YaYiYaO 2007-07-01
  • 打赏
  • 举报
回复
我就是想把式子中所有的html标签匹配出来,并不是匹配完整的字符串
我查了boost regex的文档,要找出所有匹配的字符串,是否只能重复调用regex_search,直到失败为止?
believefym 2007-06-30
  • 打赏
  • 举报
回复
[^>]
这个只匹配一个非'>'的字符,当然不能匹配整个表达式了
believefym 2007-06-30
  • 打赏
  • 举报
回复
#include <boost/regex.hpp>
#include <iostream>

bool validate_card_format(const std::string s)
{
static const boost::regex e("<.+>");
return regex_match(s, e);
}

int main()
{
std::cout << (validate_card_format("<html><body>dddd</body></html>")?"PASS":"Error") ; // PASS

system("pause");
return 0;
}

64,640

社区成员

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

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