C++-正则表达式

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
2
3
4
5
6
7
8
hou
Total Match..
houlaidewomen
Part Match:hou
Part Match:lai
Part Match:de
Part Match:wo
Part Match:men