boost regex 简单例子

本文介绍了一段使用Boost库中的正则表达式搜索特定模式的C++代码示例。通过一个具体的例子展示了如何在字符串中查找符合预定义正则表达式的部分,并逐一输出匹配项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

boost的正则表达式库是目前C++开发中普遍用到的库,下面列举regex_search的用法,该用法是搜寻指定regex expression在文本中所有能够匹配的部分,注意,regex expression中的 / 要注意变成 //进行转义

  1. #include <iostream>
  2. #include <boost/regex.hpp>
  3. using namespace std;
  4. int main( int argc, char* argv[] )
  5. {
  6.     string buf = "This is and 1=1 example or 2>1";
  7.     boost::regex exampleregex("(and|or)//s+[0-9]+//s*(>|<|=|<>|!=)//s*[0-9]+");
  8.     std::string::const_iterator start, end; 
  9.     start = buf.begin(); 
  10.     end = buf.end(); 
  11.     boost::match_results<std::string::const_iterator> what; 
  12.     boost::match_flag_type flags = boost::match_default;
  13.     while(boost::regex_search(start,end,what,exampleregex,flags))
  14.     {
  15.         cout<<string(what[0].first,what[0].second)<<endl;
  16.         start = what[0].second;
  17.     }
  18.     system("pause");
  19.     return 0;
  20. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值