regex類表示一個正則表達式,regex_match和regex_search確定一個給定字符串與一個給定的regex是否匹配,如果整個字符串與regex匹配則regex_match返回true,如果字符串中的一個子串與regex匹配,則regex_search返回true。如果它們的參數(shù)中包含smatch參數(shù),成功匹配的相關(guān)信息存在該參數(shù)中。regex_replace將給定的字符串替換為另外一種形式。
int main()
{
regex r1("[0-9]+");
smatch results1;
cout << boolalpha << regex_match("123", r1) << endl;//true
cout << regex_match("12a", r1) << endl;//false
string str1{ "abc12345de111" };
if (regex_search(str1, results1, r1)) {
cout << results1.str()<< endl;//123456
}
regex r2("[0-9]+[a-z]+");
cout << boolalpha << regex_match("123", r2) << endl;//false
cout << regex_match("12a", r2) << endl;//true
system("pause");
}
指定regex對象的選項
當我們定義一個regex或?qū)σ粋€regex調(diào)用assign為其賦新值時,可以指定一些標志來影響regex如何操作,默認值為ECMAScript ,從而使regex會使用ECMA-262規(guī)范,這是很多Web瀏覽器使用的正則表達式語言。
static constexpr flag_type icase = regex_constants::icase;//匹配過程中忽略大小寫
static constexpr flag_type nosubs = regex_constants::nosubs;//不保存匹配的子表達式
static constexpr flag_type optimize = regex_constants::optimize;//執(zhí)行速度優(yōu)先于構(gòu)造速度
static constexpr flag_type collate = regex_constants::collate;//形如 "[a-b]" 的字符范圍將對本地環(huán)境敏感
static constexpr flag_type ECMAScript = regex_constants::ECMAScript;//使用ECMA-262指定的語法
static constexpr flag_type basic = regex_constants::basic;//使用POSIX基本的正則表達式語法
static constexpr flag_type extended = regex_constants::extended;//使用POSIX擴展的正則表達式語法
static constexpr flag_type awk = regex_constants::awk;//使用POSIX版本的awk語言的語法
static constexpr flag_type grep = regex_constants::grep;//使用POSIX版本的grep的語法
static constexpr flag_type egrep = regex_constants::egrep;//使用POSIX版本的egrep的語法
int main()
{
regex r1("[a-z]+");//忽略大小寫
cout << boolalpha << regex_match("abc", r1) << endl;//true
cout << regex_match("ABC", r1) << endl;//false
regex r2("[a-z]+",regex::icase|regex::ECMAScript);//忽略大小寫
cout << boolalpha << regex_match("abc", r2) << endl;//true
cout << regex_match("ABC", r2) << endl;//true
system("pause");
}
正則表達式異常處理
如果編寫的正則表達式存在錯誤,則在運行時標準庫會拋出一個類型為regex_error的異常。
int main()
{
try {
regex r1("[a-z");
}catch (regex_error e) {
//regex_error(error_brack) : The expression contained mismatched[and].
//code : 4
cout <<e.what() <<endl<<"code:"<< e.code() << endl;
}
system("pause");
}
正則表達式類型和字符串類型
| 字符串類型 | 正則表達式類型 |
|---|---|
| string | regex,smatch,ssub_match,sregex_iterator |
| const char* | regex,cmatch,csub_match,cregex_iterator |
| wstring | wregex,wsmatch,wssub_match,wsregex_iterator |
| const wchar_t* | wregex,wcmatch,wcsub_match,wcregex_iterator |
regex迭代器
使用sregex_iterator可以獲得所有匹配結(jié)果,將sregex_iterator綁定到一個字符串和一個regex對象時,sregex_iterator會調(diào)用regex_search查找第一個匹配位置,解引用迭代器時會得到最近一次搜索結(jié)果的smatch對象,當我們遞增迭代器時,它調(diào)用regex_search查找下一個匹配。
sregex_iterator有兩個名為prefix和suffix的成員,分別返回當前匹配之前和之后部分的ssub_match對象。
int main()
{
string pattern("[0-9]+");
regex r1(pattern);
string str = "12234abc11111def2222ghj3333";
for (sregex_iterator it(str.begin(), str.end(), r1), endIt; it != endIt; ++it) {
cout <<it->prefix().str()<<" "<< it->str() <<" "<<it->suffix().str()<< endl;
}
//12234 abc11111def2222ghj3333
// abc 11111 def2222ghj3333
// def 2222 ghj3333
// ghj 3333
system("pause");
}
子表達式匹配
正則表達式中通常包含一個或多個子表達式,子表達式通常用括號表示,在使用smatch保存匹配結(jié)果時,str(),str(0)表示整體正則表達式的匹配,str(1)表示第一個子表達式的匹配,str(2)表示第二個子表達式的匹配,依此類推。
smatch包含多個ssub_match元素,位置0的元素表示整體匹配結(jié)果,其余元素表示子表達式的匹配結(jié)果,如果匹配了則matched成員為true,如果整體都沒匹配,則子表達式均不匹配。
int main()
{
string pattern("([0-9]+)([a-z]+)");
regex r(pattern);
string str1 = "12234abc";
smatch result1;
if (regex_search(str1, result1, r)) {
cout << result1.str(0) << endl;//12234abc
cout << result1.str(1) << endl;//12234
cout << result1.str(2) << endl;//abc
cout << result1.str(10000) << endl;//空字符串,不會報錯
cout << boolalpha << result1[0].matched << endl;//true
cout << result1[1].matched << endl;//true
cout << result1[2].matched << endl;//true
cout << result1[3].matched << endl;//false
}
string pattern2("([0-9]+)([a-z]+)");
regex r2(pattern2);
string str2 = "12234";
smatch result2;
regex_search(str2, result2, r2);
cout << result2[0].matched << endl;//false
cout << result2[1].matched << endl;//false
cout << result2[2].matched << endl;//false
string pattern3("([0-9]+)([a-z]+)?");//問號代表可選
regex r3(pattern3);
string str3 = "12234";
smatch result3;
regex_search(str3, result3, r3);
cout << result3[0].matched << endl;//true
cout << result3[1].matched << endl;//true
cout << result3[2].matched << endl;//false
system("pause");
}
regex_replace
regex_replace可以查找匹配正則表達式的字符串并使用其它格式來替換,符號$后跟子表達式的索引號表示一個特定的子表達式。
int main()
{
string pattern("([0-9]{4})-([0-9]{2})-([0-9]{2})");
regex r(pattern);
string date = "2019-06-28 2019-06-29";
cout << regex_replace(date, r, "$1.$2.$3") << endl;//2019.06.28 2019.06.29
system("pause");
}
默認情況下regex_replace會將未匹配的部分原樣輸出,我們可以通過指定format_no_copy不輸出未匹配部分。
int main()
{
string pattern("([0-9]{4})-([0-9]{2})-([0-9]{2})");
regex r(pattern);
string date = "當前日期:2019-06-28";
cout << regex_replace(date, r, "$1.$2.$3",regex_constants::format_no_copy) << endl;//2019.06.28
system("pause");
}