C++11,正则表达式应用

来源:互联网 发布:淘宝金牌店铺 编辑:程序博客网 时间:2024/06/11 22:12

一、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

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");







0 0
原创粉丝点击