C++正则表达式
来源:互联网 发布:淘宝滔搏运动城 真的吗 编辑:程序博客网 时间:2024/05/29 02:25
C++正则表达式regex
- C正则表达式regex
- 组件
- 使用正则表达式库
- test1查找违法拼写规则i除非在c后否则必须在e前的单词
- 指定regex对象的选项
- test2匹配指定后缀的文件名字
- 指定使用正则表达式时的错误
- 正则表达式类和输入序列类型
- regex类
- 匹配和迭代器类型
- 匹配与Regex迭代器类型
- 使用sregex_iterator
- test3查找所有违反test1中规则的单词
- 使用匹配数据
- test4prefix suffix
- smatch操作
- 使用子表达式
- test5test2只返回文件名不带扩展名
- 使用子匹配操作
- test6匹配并验证电话号码
- 使用regex_replace
- test7替换电话号码格式
- 使用sregex_iterator
头文件—regex
组件
regex_search & regex_match 参数
- seq,m,r,mft(字符序列,match对象—必须与seq兼容,regex对象,flag)
- seq,r,mft(同上)
使用正则表达式库
test1—查找违法拼写规则“i除非在c后否则必须在e前”的单词
#include<iostream>#include<regex>using namespace std;int main(){ string pattern("[^c]ei"); pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*"; regex r(pattern); smatch result; string test_str = "receipt freind theif receive"; if(regex_search(test_str,result,r)) cout<<result.str()<<endl;}
指定regex对象的选项
用法: regex r(re),regex r(re,f), r1 = re, r1.assign(re,f)
test2—匹配指定后缀的文件名字
#include<iostream>#include<regex>using namespace std;int main(){ //注意下面到反斜杠 regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$",regex::icase); smatch results; string filename; while(cin>>filename) { if(regex_search(filename,results,r)) cout<<results.str()<<endl; }}
指定使用正则表达式时的错误
如果我们编写的正则表达式存在错误,则运行时会抛出一个类型为regex_error的异常,regex_error有一个what操作描述发生了什么错误,还有一个code成员返回某个错误类型对应的编码。
正则表达式类和输入序列类型
regex类
regex类保存类型char的正则表达式,wregex类保存类型为wchar_t
匹配和迭代器类型
类型差异不仅在于字符类型,还在于序列是在string中还是在数组中
- smatch :表示string类型序列
- cmatch :表示字符数组序列
- wsmatch:表示宽字符串(string)序列
- wcmatch:表示宽字符数组
note: RE库类型必须与输入序列类型匹配
匹配与Regex迭代器类型
sregex_iterator 操作:
使用sregex_iterator
test3–查找所有违反test1中规则的单词
#include<iostream>#include<regex>using namespace std;int main(){ string pattern("[^c]ei"); pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*"; regex r(pattern); smatch result; string test_str = "receipt freind theif receive"; /* regex_search(test_str,result,r); for(smatch::iterator it = result.begin(); it != result.end(); ++it) { cout<<it->str()<<endl; }*/ for(sregex_iterator it(test_str.begin(),test_str.end(),r),end_it; it != end_it; it++) { cout<<it->str()<<endl; }}
使用匹配数据
匹配类型有名为prefix和suffix的成员,分别表示分别表示输入序列中当前的匹配之前和之后部分的ssub_match对象。一个ssub_match对象有str和length的成员,返回匹配的string和string的长度。
test4–prefix & suffix
#include<iostream>#include<regex>using namespace std;int main(){ string pattern("[^c]ei"); pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*"; regex r(pattern); smatch result; string test_str = "receipt freind theif receive"; for(sregex_iterator it(test_str.begin(),test_str.end(),r),end_it; it != end_it; it++) { auto pos = it->prefix().length(); pos = pos > 40? pos - 40 : 0; cout<<it->prefix().str().substr(pos) <<"\n\t\t>>> "<< it->str()<<" <<<\n" <<it->suffix().str().substr(0,40) <<endl; }}
smatch操作
使用子表达式
正则表达式通常包含一个或多个子表达式。正则表达式通常用括号表示子表达式。
regex r("([[:alnum:]]+)\\.(cpp|cxx|cc)$",regex::icase);
- ([[:alnum:]]+),匹配一个或多个字符的序列
- (cpp|cxx|cc),匹配文件扩展名
test5–test2只返回文件名,不带扩展名
#include<iostream>#include<regex>using namespace std;int main(){ regex r("([[:alnum:]]+)\\.(cpp|cxx|cc)$",regex::icase); smatch results; string filename; while(cin>>filename) { if(regex_search(filename,results,r)) cout<<results.str(1)<<endl; //str中参数n表示第n个子表达式,默认为0,表示整个匹配 }}
使用子匹配操作
test6–匹配并验证电话号码
#include<iostream>#include<regex>using namespace std;bool valid(const smatch &m){ if(m[1].matched) return m[3].matched && (m[4].matched == false || m[4].str() == " "); else return !m[3].matched && m[4].str() == m[6].str();}int main(){ //左"(",三位数字,右")","."或"-"或" ",三位数字,"或"-"或" ",四位数字 //"?"表示可有可无 string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})"; regex r(phone); string s; while(getline(cin,s)) { for(sregex_iterator it(s.begin(),s.end(),r),end_it; it != end_it; it++) { if(valid(*it)) cout<<"valid:"<<it->str()<<endl; else cout<<"invalid:"<<it->str()<<endl; } } return 0;}
使用regex_replace
当我们希望在输入序列中查找并替换一个正则表达式时可以调用regex_replace
test.7–替换电话号码格式
#include<iostream>#include<fstream>#include<regex>using namespace std;int main(){ ifstream ifs; string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})"; string fmt = "$2.$5.$7"; regex r(phone); ifs.open("test7.txt"); if(!ifs) { cout<<"test7.txt open error"<<endl; return -1; } string line; while(getline(ifs,line)) { cout<<regex_replace(line,r,fmt)<<endl; } return 0;}
0 0
- 正则表达式(C#)
- 正则表达式之C#
- C#中的正则表达式
- 正则表达式(linux/C++)
- C的正则表达式
- c#正则表达式
- linux c 正则表达式
- linux c 正则表达式
- linux C正则表达式
- linux c 正则表达式
- C语言正则表达式
- C语言正则表达式
- C语言正则表达式
- inux C 正则表达式
- C#----初识正则表达式
- 正则表达式(C#)
- C正则表达式
- C#,.net 正则表达式
- 在你的 composer.json 中增加 doctrine/dbal 依赖
- 2016 Multi-University Training Contest 1 GCD【RMQ+二分】
- POJ 2823 Sliding Window
- 剑指offer 替换空格
- Unity UGGUI RawImage 渲染小地图
- C++正则表达式
- 一个通用高自由度技能系统的设计(一)
- 【BZOJ1503】[NOI2004]郁闷的出纳员
- WebForm GridView分页
- qt5实现tftp和ftp的方法之五:使用Qtftp实现ftp的方法
- mac如何获取API的资源图片 和 Windows系统下如何查看API资源图片
- ftp文件下载
- The 101 Most Useful Websites
- python os.path模块