C++中字符串的正则匹配。
##include <regex>
string text;
cin >> text;
// Total Match
regex expression("[^aoeiuv]?h?[iuv]?(ai|ei|ao|ou|er|ang?|eng?|ong|a|o|e|i|u|ng|n)?");
if(regex_match(text, expression)){
cout << "Total Match.." << endl;
return 0;
}
// Part Match
smatch match;
while(regex_search(text, match, expression)){
string result = match[0];
cout << "Part Match:" << result << endl;
if(text.length() > result.length())
text.erase(text.begin(), text.begin() + result.length());
else
break;
}
上面代码中的正则表达式式匹配拼音的,测试如下:
1 | hou |