C++11--正则表达式<regex>
/*
正则表达式:一个指定的模式用来对文本中的字符串提供精确且灵活的匹配
*/
#include <regex>
using namespace std;
int main() {
string str;
while (true) {
cin >> str;
//regex e("abc.", regex_constants::icase); // . 表示除了换行符之外任意字符
//regex e("abc?"); // ? 0个或者1个前面的字符
//regex e("abc*"); // * 0个或多个前面的字符
//regex e("abc+"); // + 1个或多个前面的字符
//regex e("ab[cd]*"); // [...] 方括号中任意字符
//regex e("ab[^cd]*"); // [^...] 任意不在方括号中的字符
//regex e("ab[cd]{3,5}");
//regex e("abc|de[\]fg]"); // | 或者
//regex e("(abc)de+\\1"); // \1 第1个子串
//regex e("(ab)c(de+)\\2\\1");
//regex e("[[:w:]]+@[[:w:]]+\.com"); // [[:w:]] :字母,数字,下划线
//regex e("abc.$"); // $ 行尾
regex e("^abc.+", regex_constants::grep); // ^ 行首,切换正则表达式语法
//bool match = regex_match(str, e); //str和e精确匹配
bool match = regex_search(str, e); //str中中包含e
cout << (match? "Matched" : "Not matched") << endl << endl;
}
}
/*
正则表达式语法:
ECMAScript //C++默认
basic
extended
awk
grep
egrep
regex e("^abc.+", regex_constants::grep); // 切换语法
*/
/*************** 处理子表达式 *****************/
/*
std::match_results<> 储存详细的匹配Store the detailed matches
smatch string类型的详细的匹配Detailed match in string
smatch m;
m[0].str() 整个匹配的字符串 (同m.str(), m.str(0))
m[1].str() 第1个子串(同m.str(1))
m[2].str() 第2个子串
m.prefix() 所有匹配字符串之前的部分
m.suffix() 所有匹配字符串之后的部分
*/
int main() {
string str;
while (true) {
cin >> str;
smatch m; // typedef std::match_results<string>
regex e("([[:w:]]+)@([[:w:]]+)\.com");
bool found = regex_search(str, m, e); //只返回第一个匹配
cout << "m.size() " << m.size() << endl; //size()==子匹配个数+1
for (int n = 0; n< m.size(); n++) {
cout << "m[" << n << "]: str()=" << m[n].str() << endl;
cout << "m[" << n << "]: str()=" << m.str(n) << endl;
cout << "m[" << n << "]: str()=" << *(m.begin()+n) << endl;
}
cout << "m.prefix().str(): " << m.prefix().str() << endl;
cout << "m.suffix().str(): " << m.suffix().str() << endl;
}
}
// 多个匹配的情况
/**************** Regex Iterator ******************/
int main() {
cout << "Hi" << endl;
string str;
while (true) {
cin >> str;
regex e("([[:w:]]+)@([[:w:]]+)\.com");
sregex_iterator pos(str.cbegin(), str.cend(), e);
sregex_iterator end; // 默认构造定义了past-the-end迭代器
for (; pos!=end; pos++) {
cout << "Matched: " << pos->str(0) << endl;
cout << "user name: " << pos->str(1) << endl;
cout << "Domain: " << pos->str(2) << endl;
cout << endl;
}
cout << "=============================\n\n";
}
}
/**************** Regex Token Iterator ******************/
int main() {
cout << "Hi" << endl;
//string str = "Apple; Orange, {Cherry}; Blueberry";
string str = "boq@yahoo.com, boqian@gmail.com; bo@hotmail.com";
//regex e("[[:punct:]]+"); // 空格,数字,字母以外的可打印字符
//regex e("[ [:punct:]]+");
regex e("([[:w:]]+)@([[:w:]]+)\.com");
sregex_token_iterator pos(str.cbegin(), str.cend(), e, 0); //最后一个参数指定打印匹配结果的哪一部分,0表达整个匹配字符串,1表示第1个子串,-1表示没有匹配的部分
sregex_token_iterator end; // 默认构造定义了past-the-end迭代器
for (; pos!=end; pos++) {
cout << "Matched: " << *pos << endl;
}
cout << "=============================\n\n";
cin >> str;
}
/**************** regex_replace ******************/
// 将匹配的字符串部分替换
int main() {
cout << "Hi" << endl;
string str = "boq@yahoo.com, boqian@gmail.com; bo@hotmail.com";
regex e("([[:w:]]+)@([[:w:]]+)\.com");
regex e("([[:w:]]+)@([[:w:]]+)\.com", regex_constants::grep|regex_constants::icase );
//cout << regex_replace(str, e, "$1 is on $2");
cout << regex_replace(str, e, "$1 is on $2", regex_constants::format_no_copy|regex_constants::format_first_only);//format_no_copy不匹配的字符部分不拷贝到新串,只匹配第一个
cout << regex_replace(str, e, "$1 is on $2"); // $1表示第一个子串
std::cin >> str;
}
C++11--正则表达式<regex>的更多相关文章
- c++11 正则表达式基本使用
c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...
- C++11 | 正则表达式(4)
C++11还支持正则表达式里的子表达式(也叫分组),用sub_match这个类就行了. 举个简单的例子,比如有个字符串"/id:12345/ts:987697413/user:678254& ...
- (四)boost库之正则表达式regex
(四)boost库之正则表达式regex 正则表达式可以为我们带来极大的方便,有了它,再也不用为此烦恼 头文件: #include <boost/regex.hpp> 1.完全匹配 std ...
- 【正则表达式1】C++11正则表达式
https://www.cnblogs.com/pukaifei/p/5546968.html [正则表达式1]C++11正则表达式 头文件 #include <regex> rege ...
- 请写出正则表达式(regex),取得下列黄色部分的字符串 TEL: 02-236-9655/9659 FAX:02-236-9654 (黄色部分即02-236-9655/9659 ) ( 测试面试题)
请写出正则表达式(regex),取得下列黄色部分的字符串 TEL: 02-236-9655/9659 FAX:02-236-9654 答: package test1; import java.uti ...
- C#正则表达式Regex常用匹配
使用Regex类需要引用命名空间:using System.Text.RegularExpressions; 利用Regex类实现验证 示例1:注释的代码所起的作用是相同的,不过一个是静态方法,一个是 ...
- C#正则表达式Regex类的用法
C#正则表达式Regex类的用法 更多2014/2/18 来源:C#学习浏览量:36891 学习标签: 正则表达式 Regex 本文导读:正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串, ...
- C#正则表达式Regex类
C#正则表达式Regex类的使用 C#中为正则表达式的使用提供了非常强大的功能,这就是Regex类.这个包包含于System.Text.RegularExpressions命名空间下面,而这个命名空间 ...
- boost 正则表达式 regex
boost 正则表达式 regex 环境安装 如果在引用boost regex出现连接错误,但是引用其他的库却没有这个错误,这是因为对于boost来说,是免编译的,但是,正则这个库 是需要单独编译 ...
- python进阶11 正则表达式
python进阶11 正则表达式 一.概念 #正则表达式主要解决什么问题? #1.判断一个字符串是否匹配给定的格式,判断用户提交的又想的格式是否正确 #2.从一个字符串中按指定格式提取信息,抓取页面中 ...
随机推荐
- tarfile — Read and write tar archive files
参考: https://docs.python.org/2/library/tarfile.html http://www.jianshu.com/p/bbad16822eab #解压文件tarfil ...
- leetcode227. Basic CalculatorII
这道题是只有四则运算但是没有括号的,因此可以利用stack来存储的,并且每次使得存储的值与符号有对应的关系,最后在栈中只剩下可以利用加法进行处理的数的,注意在i=n-1的时候到达最后的部分也是需要把数 ...
- Vue的新启之笔
之前就有接触Vue这一语言,作为一个摊薄饼的我,觉得其基础性的知识体系与其他语言是相通的.且由于贵阳这一城市的地理位置的特殊性,我不得不承认想要从事软件开发这一行业,不精通一门语言不行.因为,任何一家 ...
- Sublime text3常用的插件功能和常用的快捷键
Sublime text3常用的插件功能和用法 Package control 插件管理 (使用ctrl+` 将代码复制后粘贴到代码粘贴处,按Enter没有出现错误的话就安装成功了)(ctrl+shi ...
- 栈与队列(Stack and Queue)
1.定义 栈:后进先出(LIFO-last in first out):最后插入的元素最先出来. 队列:先进先出(FIFO-first in first out):最先插入的元素最先出来. 2.用数组 ...
- 【shell编程】之基础知识-输入/输出和重定向
大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准输出,默 ...
- 【vue】vue使用Element组件时v-for循环里的表单项验证方法
转载至:https://www.jb51.net/article/142750.htm标题描述看起来有些复杂,有vue,Element,又有表单验证,还有v-for循环?是不是有点乱?不过我相信开发中 ...
- mysql新建用户在本地无法登录
新建了一个mysql用户,但是无法在本地登录,即使已经授权任一ip都可以登录,甚至特地写清楚localhost登录,还是不行,情况如下 [root@localhost zabbix-release-3 ...
- 文件I/0缓冲
设置stdio流缓冲模式 #include<stdio.h> int setvbuf(FILE *stream,char *buf,int mode,size_t size) int se ...
- MySQL--时间戳与时区问题
对于使用 timestamp 的场景,MySQL 在访问 timestamp 字段时会做时区转换,当 time_zone 设置为 system 时,MySQL 访问每一行的 timestamp 字段时 ...