您的位置:首页 > 编程语言 > C语言/C++

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
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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: