本文轉載至:https://blog.csdn.net/qq_34802416/article/details/79307102
前言
正則表達式是在字符串處理中常用和重要的工具,主要用於字符串的匹配。在C#中正則表達式的使用非常方便,但到了C++中讓我有點懵逼了,花了些時間查閱了很多資料,下面主要會寫到C++中正則表達式常用到的三個方法(全文匹配、搜索和替換)的作用以及區別。
本篇博客不涉及正則表達式語法的基本內容,如果你對正則表達式不了解,可以訪問這個鏈接獲得幫助。
文章目錄
前言
1 轉義字符
2 regex_match
2.1 基本概念
2.2 匹配結果
2.3 實例
3 regex_search
3.1 基本概念
3.2 實例
4 regex_replace
4.1 基本概念
4.2 擴展
5 匹配忽略大小寫
6 幫助網站
1 轉義字符
在講具體方法之前,我們先了解下C++中使用正則表達式需要注意的一個問題:轉義字符
cout << regex_match("123", regex("\d+")) << endl; //結果為0,需要轉義字符'\'
cout << regex_match("123", regex("\\d+")) << endl; //結果為1,完全匹配
\d:匹配一個數字字符;
+ :匹配一次或多次;
C++中必須要對表達式中的’'進行轉義,為什么?
2 regex_match
2.1 基本概念
match是全文匹配,即要求整個字符串符合匹配規則。
cout << regex_match("123", regex("\\d")) << endl; //結果為0
cout << regex_match("123", regex("\\d+")) << endl; //結果為1
上述方法返回值為bool值,主要用於if條件表達式中。
2.2 匹配結果
更多的時候我們希望能夠獲得匹配結果(字符串),對結果進行操作。這時就需要對匹配結果進行存儲,共有兩種存儲方式。
match_results<string::const_iterator> result;
smatch result; //推薦
第二種方式使用起來更簡潔、方便,推薦使用。
2.3 實例
下面看一個match方法匹配的實例,看看實際應用:
string str = "Hello_2018";
smatch result;
regex pattern("(.{5})_(\\d{4})"); //匹配5個任意單字符 + 下划線 + 4個數字
if (regex_match(str, result, pattern))
{
cout << result[0] << endl; //完整匹配結果,Hello_2018
cout << result[1] << endl; //第一組匹配的數據,Hello
cout << result[2] << endl; //第二組匹配的數據,2018
cout<<"結果顯示形式2"<<endl;
cout<< result.str() << endl; //完整結果,Hello_2018
cout<< result.str(1) << endl; //第一組匹配的數據,Hello
cout << result.str(2) << endl; //第二組匹配的數據,2018
}
//遍歷結果
for (int i = 0; i < result.size(); ++i)
{
cout << result[i] << endl;
}
result[]與result.str()這兩種方式能夠獲得相同的值,我更喜歡用數組形式的。
在匹配規則中,以括號()的方式來划分組別,實例中的規則共有兩個括號,所以共有兩組數據。
3 regex_search
3.1 基本概念
search是搜索匹配,即搜索字符串中存在符合規則的子字符串。
match與search一比較便知:
cout << regex_match("123", regex("\\d")) << endl; //結果為0
cout << regex_search("123", regex("\\d")) << endl; //結果為1
3.2 實例
直接看例子:
string str = "Hello 2018, Bye 2017";
smatch result;
regex pattern("\\d{4}"); //匹配四個數字
//迭代器聲明
string::const_iterator iterStart = str.begin();
string::const_iterator iterEnd = str.end();
string temp;
while (regex_search(iterStart, iterEnd, result, pattern))
{
temp = result[0];
cout << temp << " ";
iterStart = result[0].second; //更新搜索起始位置,搜索剩下的字符串
}
輸出結果:2018 2017
只需要利用迭代器就可以很輕松的訪問到所有匹配的結果值。
4 regex_replace
4.1 基本概念
replace是替換匹配,即可以將符合匹配規則的子字符串替換為其他字符串。
string str = "Hello_2018!";
regex pattern("Hello");
cout << regex_replace(str, pattern, "") << endl; //輸出:_2018,將Hello替換為""
cout << regex_replace(str, pattern, "Hi") << endl; //輸出:Hi_2018,將Hello替換為Hi
4.2 擴展
除了直接替換以外,還有可以用來調整字符串內容(縮短、順序等)。
string str = "Hello_2018!";
regex pattern2("(.{3})(.{2})_(\\d{4})"); //匹配3個任意字符+2個任意字符+下划線+4個數字
cout << regex_replace(str, pattern2, "$1$3") << endl; //輸出:Hel2018,將字符串替換為第一個和第三個表達式匹配的內容
cout << regex_replace(str, pattern2, "$1$3$2") << endl; //輸出:Hel2018lo,交換位置順序
$n用於表示第n組匹配數據(組這個概念在文章前面部分提到過),所以我們可以利用這種方式來將字符串的內容進行調整。
5 匹配忽略大小寫
有時我們希望能夠匹配的時候忽略大小寫,這時候就要用到Regex的語法選項了。
cout << regex_match("aaaAAA", regex("a*", regex::icase)) << endl; //結果為1
cout << regex_match("aaaAAA", regex("a*")) << endl; //結果為0
regex::icase:匹配時忽略大小寫。