C++?正則表達式的應用詳解
一、為什么要學正則表達式
若要判斷一個輸入的QQ號是否有效,如何判斷?
判斷步驟:
- 長度大于5,小于等于10;
- 首位不能為0;
- 是否為純數(shù)字?
C++string處理:
#include<iostream>
using namespace std;
int main()
{
string qq = "7466a2063";
if (qq.length() >= 5 && qq.length() <= 11)
{
// 2. 判斷是否非'0'開頭
if (qq[0] != '0')
{
// 3. 判斷是否為純數(shù)字
for each (char var in qq)
{
cout << var;
if (var < '0' || var > '9')
{
cout << "不存在" << endl;
return 0;
}
}
cout << "存在" << endl;
}
else
{
cout << "不存在" << endl;
}
}
else
{
cout << "不存在" << endl;
}
return 0;
}
雖然功能實現(xiàn)了但是非常麻煩
#include<iostream>
#include<regex>
using namespace std;
int main()
{
regex qq_reg("[1-9]\\d{4,11}");
string qq = "746632063";
smatch result;
bool ret = regex_match(qq, result, qq_reg);
cout << (ret ? "存在" : "不存在") << endl;
return 0;
}
正則表達式只需幾行代碼就行了。
是不是很方便呢?那么接下來便來看看C++如何使用正則表達式
二、正則程序庫(regex)
C++11 新增了正則表達式的標準庫支持,本文簡介 C++ 正則表達式的使用: std::regex是C++用來表示正則表達式(regular expression)的庫,它是class std::basic_regex<>針對char類型的一個特化,還有一個針對wchar_t類型的特化為std::wregex。文檔介紹

正則文法:

正則庫:
regex
表示有一個正則表達式類,比如:regex pattern("(.{3})(.{2})_(\d{4})!")
regex_match
全文匹配,要求整個字符串符合正則表達式的匹配規(guī)則。用來判斷一個字符串和一個正則表達式是否模式匹配,返回一個 bool 值,true 為匹配,false 為不匹配。匹配的含義是目標字符串必須完全和正則表達式相匹配,不能有多余的字符,如果需要部分匹配則應使用regex_search regex_search 搜索匹配,根據(jù)正則表達式來搜索字符串中是否存在符合規(guī)則的子字符串。 能和正則表達式相匹配就返回true
regex_replace
替換匹配,即可以將符合匹配規(guī)則的子字符串替換為其他字符串。要求輸入一個正則表達式,以及一個用于替換匹配子字符串的格式化字符串。這個格式化字符串可以通過轉(zhuǎn)義序列引用匹配子字符串中的部分內(nèi)容
sregex_iterator
迭代器適配器,調(diào)用regex_search來遍歷一個string中所有匹配的子串
smatch/match_results
容器類,保存在string中搜索的結(jié)果。如果匹配成功,這些函數(shù)將成功匹配的相關信息保存在給定的smatch對象中。比如:smatch results;將匹配結(jié)果存放在results中,另一種存儲方法聲明:match_resultsstring::const_iterator result
匹配(Match)
字符串處理常用的一個操作是匹配,即字符串和規(guī)則恰好對應,而用于匹配的函數(shù)為std::regex_match(),它是個函數(shù)模板
bool regex_match(string s,regex pattern)
bool regex_match(string s,smatch result,regex pattern)
bool regex_match(s.cbegin()+i,s.cend(),smatch result,regex pattern)
參數(shù)s為要匹配的字符串,pattern為匹配規(guī)則,result保存結(jié)果s.cbegin()+i,s.cend()對應s的匹配s迭代器所取的范圍。
更多的時候我們希望能夠獲得匹配結(jié)果(字符串),對結(jié)果進行操作。這時就需要對匹配結(jié)果進行存儲,共有兩種存儲方式。
match_resultsstring::const_iterator result;
smatch result; //推薦
如果需要保存結(jié)果,可以用第二種函數(shù),用smatch result保存結(jié)果。通常result[0]保存整個匹配結(jié)果,result[i]保存第i個捕獲組的匹配結(jié)果,即模式中第i個括號的匹配結(jié)果。如果沒有這樣的結(jié)果則為空??梢杂胷esult.size()查看一共有多少個匹配結(jié)果。
#include<iostream>
#include<regex>
using namespace std;
int main()
{
string str = "Hello_2021";
smatch result;
regex pattern("(.{5})_(\\d{4})"); //匹配5個任意單字符 + 下劃線 + 4個數(shù)字
if (regex_match(str, result, pattern))
{
cout << result[0] << endl; //完整匹配結(jié)果,Hello_2018
cout << result[1] << endl; //第一組匹配的數(shù)據(jù),Hello
cout << result[2] << endl; //第二組匹配的數(shù)據(jù),2018
cout << "結(jié)果顯示形式2" << endl;
cout << result.str() << endl; //完整結(jié)果,Hello_2018
cout << result.str(1) << endl; //第一組匹配的數(shù)據(jù),Hello
cout << result.str(2) << endl; //第二組匹配的數(shù)據(jù),2018
}
//遍歷結(jié)果
for (int i = 0; i < result.size(); ++i)
{
cout << result[i] << endl;
}
}
result結(jié)構(gòu)為如下圖,result[0]為匹配的字符串,result[1]為Hello result[2]為2021

result[]與result.str()這兩種方式能夠獲得相同的值,我更喜歡用數(shù)組形式的。在匹配規(guī)則中,以括號()的方式來劃分組別,實例中的規(guī)則共有兩個括號,所以共有兩組數(shù)據(jù)
搜索(Search)
搜索與匹配非常相像,其對應的函數(shù)為std::regex_search,也是個函數(shù)模板,用法和regex_match一樣,不同之處在于搜索只要字符串中有目標出現(xiàn)就會返回,而非完全匹配。
bool regex_search(string s,regex pattern)
bool regex_search(string s,smatch result,regex pattern)
bool regex_search(s.cbegin()+i,s.cend(),smatch result,regex pattern) //從字符串的某個位置開始匹配?
string str = "Hello 2018, Bye 2017";
smatch result;
regex pattern("\\d{4}"); //匹配四個數(shù)字
//迭代器聲明
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; //更新搜索起始位置,搜索剩下的字符串
}
輸出結(jié)果:2018 2017
搜索給定字符串中是否存在與模式匹配的子串,如果存在則返回true。
同樣可以用smatch result記錄結(jié)果,但不同的是result[0]記錄的是整個字符串中從左往右第一個匹配模式的子串。
假如有多個子串符合模式,若想知道result[0]中存儲的是第幾個子串,可以用result.position()函數(shù),返回數(shù)從0開始。
替換(Replace)
replace是替換匹配,即可以將符合匹配規(guī)則的子字符串替換為其他字符串。
string regex_replace(string s,regex p,string replace_str)
string str = "Hello_2021!";
regex pattern("Hello");
cout << regex_replace(str, pattern, "") << endl;
cout << regex_replace(str, pattern, "Hi") << endl;
到此這篇關于C++ 正則表達式的應用詳解的文章就介紹到這了,更多相關C++ 正則表達式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++實現(xiàn)LeetCode(117.每個節(jié)點的右向指針之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(117.每個節(jié)點的右向指針之二),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07

