c++ 11 正则表达式

常用的方法 regex_match regex_search regex_replace 等.

regex_match 要求正则表达式必须与模式串完全匹配,例如:

string str = "o ";
regex pattern("o\\s"); bool matched = regex_match(str,pattern);
if (matched) {
cout << "matched.." << endl;
}else{
cout << "not matched." << endl;
}

上面就可以匹配.如果修改一下下:

string str = "o 1";
regex pattern("o\\s"); bool matched = regex_match(str,pattern);
if (matched) {
cout << "matched.." << endl;
}else{
cout << "not matched." << endl;
}

就是 not matched. 了

regex_match 包含子模式的匹配,并且显示匹配结果,主要使用了

match_results<string::const_iterator> result

存储匹配后的结果.

    string str = "1:2:33:344";
regex pattern("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})"); match_results<string::const_iterator> result;
bool matched = regex_match(str,result,pattern);
if (matched) {
cout << "matched.." << endl;
printf("result.size = %d\n",(int)result.size());
for (int i = 0; i < result.size(); ++i) {
printf("result[%d]:%s\n",i,result[i].str().c_str());
}
}else{
cout << "not matched." << endl;
}

输出:


matched..


result.size = 5


result[0]:1:2:33:344


result[1]:1


result[2]:2


result[3]:33


result[4]:344


主意打印的结果result[0]

regex_search 只要求存在匹配项就可以.

string str = "o 1";
regex pattern("\\w\\s"); bool matched = regex_search(str,pattern);
if (matched) {
cout << "matched.." << endl;
}else{
cout << "not matched." << endl;
}

输出: matched..

如果只想输出第一个匹配项,使用 smatch 存储结果

string str = "o 1s;23235;t;dsf;sd 66 ";
regex pattern(";"); smatch result;
bool matched = regex_search(str,result,pattern);
if (matched) {
cout << "matched.." << endl;
cout << "result.size:" << result.size() << endl;
for (int i = 0; i < result.size(); ++i) {
printf("result[%d]:\t%s\n",i,result[i].str().c_str());
}
}else{
cout << "not matched." << endl;
}

输出结果为:

matched..

result.size:1

result[0]: ;

如果想输出所有的匹配结果,则需要使用 sregex_token_iterator

string str = "o 1s 23235 t ds f sd 66 ";
regex pattern("\\w\\s"); sregex_token_iterator end;
sregex_token_iterator start(str.begin(),str.end(),pattern); while (start != end) {
cout << (*start) << endl;
++start;
}

输出为:

o

s

5

t

s

f

d

6

regex_replace 方法替换掉匹配上的

string str = "o 1s 23235 t ds f sd 66 ";
regex pattern("\\w\\s"); bool matched = regex_search(str,pattern);
if (matched) {
cout << "matched ." << endl;
auto newStr = regex_replace(str,pattern,"---");
printf("newStr : %s\n",newStr.c_str());
}else{
cout << "not matched ." << endl;
}

输出结果为:

matched .

newStr : ---1---2323------d------s---6---

上面就是c++11 正则表达式的基本使用.

内容基本来自于: 这个作者

最新文章

  1. laravel框架中容器类简化代码-摘自某书
  2. 手机APP功能测试经验分享2016.06.06
  3. C# 调用存储过程操作 OUTPUT参数和Return返回值
  4. MVC中SelectList和@Html.DropDownList(&quot;MainDuty_UserId&quot;,&quot;请选择&quot;)的运用
  5. lnmp.org一键安装包
  6. oc语言--protocol(协议)
  7. android中怎么调整字体的间距和行间距
  8. jQuery防京东浮动网站楼层导航代码
  9. Css详解之(伪类选择器)
  10. NGINX按天生成日志文件的简易配置
  11. ElasticSearch日常使用脚本
  12. 关于PHP的那些坑
  13. BGP - 5,BGP属性
  14. Linux_02
  15. SeaJS之use函数
  16. C#webBrowser使用代理服务器的方法winform
  17. 第一模块第一章 review
  18. cron表达式举例
  19. 如何用代码方式获取Web.config中system.serviceModel/client节点的address
  20. linux的rpm教程

热门文章

  1. iOS coredata 数据库升级 时报Can&#39;t find model for source store
  2. ios swfit 由继承UIButton了解类的构造方法
  3. vs2013 控制台程序exe图标
  4. ACM/ICPC 之 分治法入门(画图模拟:POJ 2083)
  5. Effective C++ -----条款10: 令operator=返回一个reference to *this
  6. Parallels Destop软件配置
  7. poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
  8. HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场
  9. oracle两时间相减得到相差的时间
  10. python基础——多重继承