c++11 正则表达式基本使用
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 正则表达式的基本使用.
内容基本来自于: 这个作者
c++11 正则表达式基本使用的更多相关文章
- C++11 | 正则表达式(4)
C++11还支持正则表达式里的子表达式(也叫分组),用sub_match这个类就行了. 举个简单的例子,比如有个字符串"/id:12345/ts:987697413/user:678254& ...
- 【正则表达式1】C++11正则表达式
https://www.cnblogs.com/pukaifei/p/5546968.html [正则表达式1]C++11正则表达式 头文件 #include <regex> rege ...
- python进阶11 正则表达式
python进阶11 正则表达式 一.概念 #正则表达式主要解决什么问题? #1.判断一个字符串是否匹配给定的格式,判断用户提交的又想的格式是否正确 #2.从一个字符串中按指定格式提取信息,抓取页面中 ...
- 理解C++11正则表达式(2)
今天有幸(2016/3/19)在上海参加了C++交流会,见到了梦寐已久想见的台湾C++大神老师侯捷,心情十分的激动.侯老师对C++理解的深刻,让人叹为观止.以为他教学的严谨,说话方式娓娓道来,听着非常 ...
- 理解c++11正则表达式 (1)
概要 C++11提出了正则表达式这个概念,只需在头文件中包含#include<regex>即可.我们可以完成: Match 将整个输入拿来比对匹配某个正则表达式 Search 查找与正则表 ...
- C++11 正则表达式——基础知识介绍
C++11开始支持正则表达式,使得处理文本更加简洁方便.C++11 支持六种正则表达式语法:ECMAScript, basic(POSIX Basic Regular Expressions), ex ...
- C++11 正则表达式简单运用
正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex.regexp.RE.regexps.regexes.regexen. 正则表达式是一种 ...
- C++11 正则表达式——实例系统(转载)
一.用正则表达式判断邮箱格式是否正确 1 #include <regex> #include <iostream> #include <string> bool i ...
- C++11正则表达式初探
C++正则表达式 在此之前都没有了解过C++的正则,不过现在大多数赛事都支持C++11了,因此有必要学习一下,用于快速A签到题. 所在头文件 #include<regex> 正则表达式语法 ...
随机推荐
- response content-type json
2015年11月3日 15:47:43 百度知道:ajax开发中在请求服务器端的响应时, 对于每一种返回类型 规范的做法是要在服务端指定response的contentType 常遇到下面的几种情况: ...
- eclipse中手动导入DTD文件的方式
DTD一般应用在应用程序中定义数据交换类型的文档,一般用在xml配置文件中,有些时候在eclipse中并不能加载一些提示,这个时候需要手动导入,导入方法如下: 1.首先根据声明的网址下载.dtd的文件 ...
- apache官网怎样下载apache HTTP Server服务器
我相信有些朋友刚用apache服务器时,都希望从官网上下载,而面对着官网上众多的项目和镜像以及目录,也许有点茫然.下面是具体步骤 第一步:打开apache官网 第二步:点击右上角Download,出现 ...
- SAP ALV显示并打印(非OO方式)
*&---------------------------------------------------------------------* *& Report Z_SD_CPF ...
- Git-windows安装包
下载地址 http://192.168.168.230/sw-search-sp/soft/4e/30195/Git-2.7.2-32-bit_setup.1457942412.exe 来自为知笔记( ...
- 4.openstack之mitaka搭建glance镜像服务
部署镜像服务 一:安装和配置服务 1.建库建用户 mysql -u root -p CREATE DATABASE glance; GRANT ALL PRIVILEGES ON glance.* T ...
- javascript中元素的scrollLeft和scrollTop属性说明
再说意义之前,前说一下这两个属性的适用范围: 注意:这两个属性只能用于元素设置了overflow的css样式中.否者这两个属性没有任何意义.且overflow的值不能为visible,但可以为hidd ...
- sqlplus 设置
set heading offset line 40001.设置页面显示总行数show pagesize; //首先查看目前的pagesize,默认是14set pagesize 100; //将pa ...
- poj 3661 Running
题意:给你一个n,m,n表示有n分钟,每i分钟对应的是第i分钟能跑的距离,m代表最大疲劳度,每跑一分钟疲劳度+1,当疲劳度==m,必须休息,在任意时刻都可以选择休息,如果选择休息,那么必须休息到疲劳度 ...
- Android Stutio -- 编译报错: Error:File path too long on Windows, keep below 240
原文:http://blog.csdn.net/qq_28195645/article/details/51556975 目录太长,解决办法: 1.将整个project移到更外层的目录,直至没有报错, ...