【正则表达式1】C++11正则表达式
https://www.cnblogs.com/pukaifei/p/5546968.html
【正则表达式1】C++11正则表达式
头文件
#include <regex>
regex_match:整个字符串是否匹配

regex reg1("\\w+day");
string s1 = "saturday";
string s2 = "saturday and sunday";
smatch r1;
smatch r2;
cout << boolalpha << regex_match(s1, r1, reg1) << endl; //true
cout << boolalpha << regex_match(s2, r2, reg1) << endl; //false
cout << "s1匹配结果:" << r1.str() << endl; //saturday
cout << "s2匹配结果:" << r2.str() << endl; //空
cout << endl;

regex_match:只返回第一个匹配结果

smatch rr1;
smatch rr2;
cout << boolalpha << regex_search(s1, rr1, reg1) << endl; //true
cout << "s1匹配结果:" << rr1.str() << endl; //saturday
cout << boolalpha << regex_search(s2, rr2, reg1) << endl; //true
cout << "s1匹配结果:" << rr2.str() << endl; //saturday
cout << endl;

iterator:返回多个匹配结果
类似于指针,调用成员要用"->"

cout << "iterator结果:" << endl;
sregex_iterator it(s2.begin(), s2.end(), reg1);
sregex_iterator end;
for(; it != end; ++it)
{
cout << it->str() << endl;
//cout << *it << endl; 错误
} cout << "token_iterator结果:" << endl;
sregex_token_iterator tit(s2.begin(), s2.end(), reg1);
sregex_token_iterator tend;
for(; tit != tend; ++tit)
{
cout << tit->str() << endl;
cout << *tit << endl;
}

子表达式匹配

regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");
string ip = "0:11:222:333";
smatch m;
regex_match(ip, m, reg2);
cout << "输出:str()" << endl;
cout << m.str() << endl; //0:11:222:333
cout << m.str() << endl; //0
cout << m.str() << endl; //11
cout << m.str() << endl; //222
cout << m.str() << endl; //333 cout << "输出:[i]" << endl; //结果同上
cout << m[] << endl;
cout << m[] << endl;
cout << m[] << endl;
cout << m[] << endl;
cout << m[] << endl;

多个匹配结果

string ip2 = "0:11:222:333 4:55:66:7";
sregex_iterator ip_it(ip2.begin(), ip2.end(), reg2);
sregex_iterator ip_end;
for(; ip_it != ip_end; ++ip_it)
{
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
}

总的程序:
#include <iostream>
#include <string>
#include <regex>
using namespace std; int main()
{
//regex_match匹配整个字符串
regex reg1("\\w+day");
string s1 = "saturday";
string s2 = "saturday and sunday";
smatch r1;
smatch r2;
cout << boolalpha << regex_match(s1, r1, reg1) << endl;
cout << boolalpha << regex_match(s2, r2, reg1) << endl;
cout << "s1匹配结果:" << r1.str() << endl;
cout << "s2匹配结果:" << r2.str() << endl;
cout << endl; //regex_match只返回第一个匹配结果
smatch rr1;
smatch rr2;
cout << boolalpha << regex_search(s1, rr1, reg1) << endl;
cout << "s1匹配结果:" << rr1.str() << endl;
cout << boolalpha << regex_search(s2, rr2, reg1) << endl;
cout << "s1匹配结果:" << rr2.str() << endl;
cout << endl; //使用iterator返回多个匹配结果
//结果要用->
cout << "iterator结果:" << endl;
sregex_iterator it(s2.begin(), s2.end(), reg1);
sregex_iterator end;
for(; it != end; ++it)
{
cout << it->str() << endl;
//cout << *it << endl; 错误
} cout << "token_iterator结果:" << endl;
sregex_token_iterator tit(s2.begin(), s2.end(), reg1);
sregex_token_iterator tend;
for(; tit != tend; ++tit)
{
cout << tit->str() << endl;
cout << *tit << endl;
} //子表达式匹配
regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");
string ip = "0:11:222:333";
smatch m;
regex_match(ip, m, reg2);
cout << "输出:str()" << endl;
cout << m.str() << endl;
cout << m.str() << endl;
cout << m.str() << endl;
cout << m.str() << endl;
cout << m.str() << endl; cout << "输出:[i]" << endl;
cout << m[] << endl;
cout << m[] << endl;
cout << m[] << endl;
cout << m[] << endl;
cout << m[] << endl; //输出结果同上
//regex_search(ip, m, str2);
cout << endl;
string ip2 = "0:11:222:333 4:55:66:7";
sregex_iterator ip_it(ip2.begin(), ip2.end(), reg2);
sregex_iterator ip_end;
for(; ip_it != ip_end; ++ip_it)
{
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
cout << ip_it->str() << endl;
} return ; }
参考:
http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438191.html
http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438215.html

【正则表达式1】C++11正则表达式的更多相关文章
- C++11 | 正则表达式(4)
C++11还支持正则表达式里的子表达式(也叫分组),用sub_match这个类就行了. 举个简单的例子,比如有个字符串"/id:12345/ts:987697413/user:678254& ...
- c++11 正则表达式基本使用
c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...
- 理解c++11正则表达式 (1)
概要 C++11提出了正则表达式这个概念,只需在头文件中包含#include<regex>即可.我们可以完成: Match 将整个输入拿来比对匹配某个正则表达式 Search 查找与正则表 ...
- C++11 正则表达式简单运用
正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex.regexp.RE.regexps.regexes.regexen. 正则表达式是一种 ...
- python进阶11 正则表达式
python进阶11 正则表达式 一.概念 #正则表达式主要解决什么问题? #1.判断一个字符串是否匹配给定的格式,判断用户提交的又想的格式是否正确 #2.从一个字符串中按指定格式提取信息,抓取页面中 ...
- linux正则表达式之-基础正则表达式(基于grep)
linux正则表达式: 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法,如:假设@代表123456,!代表abcde.通过定义的这些特殊符号的铺助,系统管理员就可以快速的过滤,替换或 ...
- RegExp正则表达式规则以及常用正则表达式
html,body { font-family: "SF UI Display", ".PingFang SC", "PingFang SC" ...
- Java 正则表达式详解_正则表达式
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- python正则表达式模块re:正则表达式常用字符、常用可选标志位、group与groups、match、search、sub、split,findall、compile、特殊字符转义
本文内容: 正则表达式常用字符. 常用可选标志位. group与groups. match. search. sub. split findall. compile 特殊字符转义 一些现实例子 首发时 ...
随机推荐
- Prolog学习:基本概念
上一篇对Prolog有了一个感性的认识,今天介绍下Prolog中一些基本概念,想要用Prolog解决一些实际问题之前必须要先了解它们.这些概念在<七周七语言>这本书中都有介绍,我简单提炼汇 ...
- e809. 在菜单中使菜单项分开
A separator typically appears as a horizontal line. It is used to group related sets of menu items i ...
- 图形界面至少要有一个顶级Swing容器
图形界面至少要有一个顶级Swing容器 顶级Swing容器为其它Swing组件在屏幕上的绘制和处理事件提供支持 常用的顶级容器: JFrame(框架):表示主程序窗口 JDialog(对话框):每个J ...
- Radix-64编码简介
本文介绍Radix-64编码,PGP和S/MIME均使用了Radix-64编码技术,rfc4880的Chap 6有关于Radix-64的详细描述. Radix-64编码基于Base64编码技术,由两部 ...
- Erlang Trace机制
从FTP模块学习先进的诊断技术(Erlang Trace机制) http://blog.yufeng.info/archives/466
- hibernate 中的session和事务(Transaction)
在使用hibernate开发时,遇到最多的就是session与事务,那么他们两个有什么关系呢?下面我来抛砖引玉: 1.session是hibernate中的以及缓存机制,是用来对数据进行增删改查的一个 ...
- linux命令中的head命令
head命令和tail命令就像他的名字一样浅显易懂,它是用来显示开头或者结尾某个数量的文字区块,head用来显示档案的开头至标准输出当中,而tail想当然就是查看档案的结尾. 命令格式 head [ ...
- 【转】Spring Boot干货系列:(二)配置文件解析
转自:Spring Boot干货系列:(二)配置文件解析 前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用"习惯优于配置"(项目中存在大量的配置,此 ...
- 关于jq ajax封装以及ajax上传Webapi
jq的ajax完整版本 $(function () { fileChange(); }); function fileChange() { $('#fileinput').change ...
- USBWebServer 中文便携版 快速搭建 PHP/MySQL 网站服务器环境
如果你是一位 WEB 开发者,或正在学习网页编程,你一定会发现,每到一台新电脑上想要在本地调试测试/运行网站代码都得搭建配置一遍 WAMP (Win.Apache.PHP.MySQL) 环境简直烦透了 ...