boost之regex使用范例

#include <regex>
#include <list> 

 对wchar_t*字符串的search调用方法:

boost::wcmatch match;

boost::wregex reg(L"\\d{3}你");
boost::regex_search(L"345你好啊!", match, reg);
bool mt0 = match[0].matched;
bool mt1 = match[1].matched;
bool mt2 = match[2].matched;
int ln = match.length();

 

 对std::wstring调用search方法

boost::wsmatch match;
std::wstring str(L"1、运行IVM解器安装程序\
ivmdecoder1.7.1.cn.exe\
2、直接使用鼠标左键双击ivm视频文件即可观看");
boost::wregex reg(L"\\w+(\\d\\.\\d\\.\\d)(\\.\\w+\\.\\w+)");
boost::regex_search(str, match, reg);
int match_size = match.size();
bool mt0 = match[0].matched;
bool mt1 = match[1].matched;
bool mt2 = match[2].matched;
bool mt3 = match[3].matched;
int ln = match.length();
std::wstring s1 = match[0];
std::wstring s2 = match[1];
std::wstring s3 = match[2];

std::wstring s4 = match[3]; 

 因为表达式里有两个分组,所以加上整个字符串共有3个match

 最后一个也就是match[3]是没有值的

 

 对std::wstring调用match方法:

boost::wsmatch match;
boost::wregex reg(L"[^\\d]+(\\d{4})[^\\d]+");
std::wstring ws(L"大家4578好,哈哈!");
bool match_status = boost::regex_match(ws, match, reg);
if (match_status) {

 

对std::wstring 调用replace方法

boost::wregex reg(L"\\d{3}你",
boost::wregex::icase|boost::wregex::perl);
std::wstring str(L"345你好啊!");

 str=boost::regex_replace(str, reg, L"不"); 

 

对std::wstring调用split方法,得到多个std::wstring

std::wstring ws1;
std::list<std::wstring> list;
boost::wregex e(L";|\\|",
             boost::regex::normal | boost::regbase::icase);
std::wstring str = L"你好;他好;我好;哈哈|来吧";
boost::regex_split(std::back_inserter(list), str, e);
std::wstring s;
while(list.size())
{
s = *(list.begin());
list.pop_front();
//cout << s << endl;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值