C++11,正则表达式应用
2016-03-03 15:10
281 查看
一、basic_regex类
1、typedef basic_regex<char> regex;
2、typedef basic_regex<wchar_t> wregex;
实例化正则对象:regex reg("[\\d]{3}");
二、match_results类
1、typedef match_results<const char*> cmatch;
2、typedef match_results<const wchar_t*> wcmatch;
3、typedef match_results<string::const_iterator> smatch;
4、typedef match_results<wstring::const_iterator> wsmatch;
实例化匹配结果对象:smatch;
三、sub_match类
1、typedef sub_match<const char*> csub_match;
2、typedef sub_match<const wchar_t*> wcsub_match;
3、typedef sub_match<string::const_iterator> ssub_match;
4、typedef sub_match<wstring::const_iterator> wssub_match;
获取一个sub_match:ssub_match = smatch[0];
四、regex_iterator类
typedef regex_iterator<const char*> cregex_iterator;
typedef regex_iterator<const wchar_t*> wcregex_iterator;
typedef regex_iterator<string::const_iterator> sregex_iterator;
typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
五、regex_token_iterator类:
typedef regex_token_iterator<const char*> cregex_token_iterator;
typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
用法:
1、regex_match,需要完全匹配str,才返回true
4、regex_replace
1、typedef basic_regex<char> regex;
2、typedef basic_regex<wchar_t> wregex;
实例化正则对象:regex reg("[\\d]{3}");
二、match_results类
1、typedef match_results<const char*> cmatch;
2、typedef match_results<const wchar_t*> wcmatch;
3、typedef match_results<string::const_iterator> smatch;
4、typedef match_results<wstring::const_iterator> wsmatch;
实例化匹配结果对象:smatch;
三、sub_match类
1、typedef sub_match<const char*> csub_match;
2、typedef sub_match<const wchar_t*> wcsub_match;
3、typedef sub_match<string::const_iterator> ssub_match;
4、typedef sub_match<wstring::const_iterator> wssub_match;
获取一个sub_match:ssub_match = smatch[0];
四、regex_iterator类
typedef regex_iterator<const char*> cregex_iterator;
typedef regex_iterator<const wchar_t*> wcregex_iterator;
typedef regex_iterator<string::const_iterator> sregex_iterator;
typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
五、regex_token_iterator类:
typedef regex_token_iterator<const char*> cregex_token_iterator;
typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
用法:
1、regex_match,需要完全匹配str,才返回true
regex reg("[\\d]{3}"); smatch result; BOOL bRet = regex_match(str, result,reg);2、regex_search,局部匹配str,即可返回true
regex reg("[\\d]{3}"); smatch result; BOOL bRet = regex_search(str, result,reg);3、regex_iterator
// instr中,从开头到结尾,跟reg匹配的string,存入迭代器中 string str = "123,456,asdfsdf"; regex reg("[\\d]{3}"); sregex_iterator iter(str.begin(), str.end(), reg),end; while (iter != end) { cout << iter->str() << endl; ++iter; }
4、regex_replace
string str = "123,456,asdfsdf"; regex reg("[\\d]{3}"); string strRes = regex_replace(str,reg,"asdas");
相关文章推荐
- C语言字符判断、大小写互转(五)
- ‘之’字形打印矩阵
- leetcode - Count of Smaller Numbers After Self
- TypeError: No to_python (by-value) converter found for C++ type:
- 浅析C++中的this指针
- C++获得主机IP地址
- C++ 运算符优先级
- c++重载、覆盖、隐藏的区别和执行方式
- 《编写高质量代码:改善C++程序的150个建议》读书笔记4
- 1089. Insert or Merge (25)
- 【转】C/C++基本编程技能
- C/C++编程技能【整理】
- C语言之volatile【整理】
- C语言 复杂队列(链表队列)
- C语言snprintf()函数:将格式化的数据写入字符串—sprintf()
- c++的POD类型详解
- 对位操作的C++的简单封装
- 【C++】函数缺省参数的作用
- C#、C++、JAVA中虚函数和抽…
- 【VC++ 程序共享】 游戏…