关于正则表达式的语法和字符含义,网上已经有很不错的博客教学,我当初参考的是
读懂正则表达式就这么简单 - Zery - 博客(http://www.cnblogs.com/zery/p/3438845.html)
正则表达式 – 语法 | 菜鸟教程 (http://www.runoob.com/regexp/regexp-syntax.html)
我在这里重点说明如何使用C++的regex库完成正则匹配,正则查找,正则替换三种操作
首先是头文件 1 #include<regex> 2 using namespace std; 正则表达式声明 string str("\\d{4}");regex pattern(str,regex::icase);注意与一般应用正则表达式不同,这里的转义符号要用“\\”
匹配结果存放变量声明 1 //第一种存储方式 2 match_results<string::const_iterator> result; 3 //第二种存储方式 4 smatch result;这两个类都可以存储匹配得到的结果,建议使用第二种,比较方便
数据准备 1 //文本数据 2 string str="1994 is my birth year"; 正则操作 正则匹配 1 //正则匹配 2 string regex_str2("(\\d{4}).*"); 3 regex pattern2(regex_str2,regex::icase); 4 5 if(regex_match(str,result,pattern2)){ 6 cout<<result[0]<<endl; 7 cout<<result[1]<<endl; 8 }注意正则匹配的运算规则是先检查正则表达式是否与文本数据一致,只有在一致的条件下才会将匹配结果送入result中。例如将正则表达式改为("\\d{4}"),返回值为FALSE,result中根本没有结果。下图是运行结果。我们从中看出result[0]是完整的文本,result[1]是第一个分组匹配的数据。如果正则表达式有n个分组,result的size也就是n+1个
正则查找 1 //文本数据 2 string str="1994 is my birth year"; 3 //正则表达式 4 string regex_str("\\d{4}"); 5 regex pattern1(regex_str,regex::icase); 6 7 //迭代器声明 8 string::const_iterator iter = str.begin(); 9 string::const_iterator iterEnd= str.end(); 10 string temp; 11 //正则查找 12 while (std::regex_search(iter,iterEnd,result,pattern1)) 13 { 14 temp=result[0]; 15 cout<<temp<<endl; 16 iter = result[0].second; //更新搜索起始位置 17 }
首先声明迭代器,在用while循环查找,每一次查找只会匹配一个结果
正则替换 //正则替换 std::regex reg1("\\d{4}"); string t("1993"); str = regex_replace(str,reg1,t); //trim_left cout<<str<<endl;在str查找匹配的文本,并用t中的数据替换。经检验,这个函数会遍历整个文本变量,也就是文本变量中所有符合正则表达式的数据都会被替换
以上就是我的经验总结,希望能帮到你。
最后附上所有代码
1 int main(){ 2 3 //第一种存储方式 4 //match_results<string::const_iterator> result; 5 //第二种存储方式 6 smatch result; 7 8 //文本数据 9 string str="1994 is my birth year 1994"; 10 //正则表达式 11 string regex_str("\\d{4}"); 12 regex pattern1(regex_str,regex::icase); 13 14 //迭代器声明 15 string::const_iterator iter = str.begin(); 16 string::const_iterator iterEnd= str.end(); 17 string temp; 18 //正则查找 19 while (std::regex_search(iter,iterEnd,result,pattern1)) 20 { 21 temp=result[0]; 22 cout<<temp<<endl; 23 iter = result[0].second; //更新搜索起始位置 24 } 25 26 //正则匹配 27 string regex_str2("(\\d{4}).*"); 28 regex pattern2(regex_str2,regex::icase); 29 30 if(regex_match(str,result,pattern2)){ 31 cout<<result[0]<<endl; 32 cout<<result[1]<<endl; 33 } 34 35 //正则替换 36 std::regex reg1("\\d{4}"); 37 string t("1993"); 38 str = regex_replace(str,reg1,t); //trim_left 39 cout<<str<<endl; 40 41 return 0; 42 }分类: C++, 正则表达式 好文要顶 关注我 收藏该文 上官栋 关注 - 5 粉丝 - 2 +加关注 1 0 « 上一篇: 独热编码OneHotEncoder简介 » 下一篇: 支持向量机SMO算法总是死循环 posted @ 2017-11-03 15:55 上官栋 阅读( 587) 评论( 0) 编辑 收藏 刷新评论 刷新页面 返回顶部 注册用户登录后才能发表评论,请 登录 或 注册, 访问网站首页。 【推荐】超50万VC++源码: 大型组态工控、电力仿真CAD与GIS源码库! 【活动】2050 大会 - 年青人因科技而团聚(5.26-5.27 杭州·云栖小镇) 【活动】缺钱怎么办?5万内你尽管花! 【推荐】华为云DevCloud精彩活动集结,重磅福利,免费领取! 【活动】腾讯云云服务器新购特惠,5折上云 【大赛】2018首届“顶天立地”AI开发者大赛 最新IT新闻: · 苹果:iPhone有数百个原型机 我们从不抄袭 · 三星电子在韩员工数量突破10万大关 月薪平均4.5万元 · 老罗看了会流泪,这才是昨晚应该刷屏的生产力工具 · 微软下半年发布低端Surface 挑战329美元新iPad · 如何安全乘坐网约车?公安部发微博支招 » 更多新闻... 最新知识库文章: · 评审的艺术——谈谈现实中的代码评审 · 如何高效学习 · 如何成为优秀的程序员? · 菜鸟工程师的超神之路 -- 从校园到职场 · 如何识别人的技术能力和水平? » 更多知识库文章...
