封裝常用正則表達(dá)式的用法
regexhelper.h
#ifndef REGEX_HELPER_H_INCLUDE
#define REGEX_HELPER_H_INCLUDE
#include<string>
#include<vector>
namespace Framework{
class RegexHelper
{
public:
RegexHelper();
virtual ~RegexHelper();
/*
* 是否包含匹配字符串
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
*/
static bool IsMatch(const char* input,const char* pattern);
/*
* 獲取首個(gè)匹配字符串或其字串
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
* @param:group 子捕獲組
*/
static std::string Match(const char* input,const char* pattern,int group = 0);
/*
* 獲取首個(gè)匹配字符串所有捕獲組
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
* @param: results 輸出的字符串?dāng)?shù)組
*/
static int Match(const char* input,const char* pattern,std::vector<std::string>& results);
/*
* 匹配字符串?dāng)?shù)目
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
*/
static int Matches(const char* input,const char* pattern);
/*
* 輸出所有匹配字符串或其捕獲組
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
* @param: results 輸出的字符串?dāng)?shù)組
* @param:group 捕獲組
*/
static int Matches(const char* input,const char* pattern,std::vector<std::string>& results,int group = 0);
/*
* 替換首個(gè)匹配字符串
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
* @param:repValue 被替換值,可以是捕獲組的組合
*/
static std::string ReplaceFirst(const char* input,const char* pattern,const char* repValue);
/*
* 替換所有匹配字符串
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
* @param:repValue 被替換值,可以是捕獲組的組合
*/
static std::string ReplaceAll(const char* input,const char* pattern,const char* repValue);
/*
* 分割字符串并輸出結(jié)果
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
* @param: results 輸出的字符串?dāng)?shù)組
*/
static int Split(const char* input,const char* pattern,std::vector<std::string>& results);
/*
* 分割字符串并根據(jù)捕獲組輸出
* @param: input 輸入字符串
* @param:pattern 正則表達(dá)式
* @param:subs 捕獲組
* @param: results 輸出的字符串?dāng)?shù)組
*/
static int Split(const char* input,const char* pattern,std::vector<int>& subs,std::vector<std::string>& results);
protected:
private:
};
}
#endif // REGEX_HELPER_H_INCLUDE
regexhelper.cpp
#include "regexhelper.h"
#include<boost/regex.hpp>
namespace Framework{
RegexHelper::RegexHelper()
{
//ctor
}
RegexHelper::~RegexHelper()
{
//dtor
}
bool RegexHelper::IsMatch(const char* input,const char* pattern)
{
boost::regex reg( pattern , boost::regex::perl|boost::regex::icase);
bool ret = boost::regex_search( input , reg);
return ret;
}
std::string RegexHelper::Match(const char* input,const char* pattern,int group)
{
if(group < 0)group = 0;
boost::cmatch mat;
boost::regex reg( pattern , boost::regex::perl|boost::regex::icase);
bool success = boost::regex_search( input, mat, reg);
if(success){
if(mat[group].matched){
return std::string(mat[group]);
}
}
return std::string("");
}
int RegexHelper::Match(const char* input,const char* pattern,std::vector<std::string>& results)
{
boost::cmatch mat;
boost::regex reg( pattern , boost::regex::perl|boost::regex::icase );
bool success =boost::regex_search( input, mat, reg);
int total = 0;
if(success){ //如果匹配成功
//cout << "match success" << endl;
//顯示所有子串
for(boost::cmatch::iterator itr=mat.begin(); itr!=mat.end(); ++itr){
// 指向子串對(duì)應(yīng)首位置 指向子串對(duì)應(yīng)尾位置 子串內(nèi)容
//cout << itr->first-szStr << ' ' << itr->second-szStr << ' ' << *itr << endl;
results.push_back(std::string(*itr));
total++ ;
}
}
return total;
}
int RegexHelper::Matches(const char* input,const char* pattern)
{
boost::regex reg( pattern, boost::regex::perl|boost::regex::icase); //查找字符串里的數(shù)字
boost::cregex_iterator itrBegin = make_regex_iterator(input,reg); //(szStr, szStr+strlen(szStr), reg);
boost::cregex_iterator itrEnd;
int total = 0;
for(boost::cregex_iterator itr=itrBegin; itr!=itrEnd; ++itr){
//cout << (*itr)[0].first-szStr << ' ' << (*itr)[0].second-szStr << ' ' << *itr << endl;
total++;
}
return total;
}
int RegexHelper::Matches(const char* input,const char* pattern,std::vector<std::string>& results,int group)
{
if(group < 0)group = 0;
boost::regex reg( pattern, boost::regex::perl|boost::regex::icase); //查找字符串里的數(shù)字
boost::cregex_iterator itrBegin = make_regex_iterator(input,reg); //(szStr, szStr+strlen(szStr), reg);
boost::cregex_iterator itrEnd;
int total = 0;
for(boost::cregex_iterator itr=itrBegin; itr!=itrEnd; ++itr){
//cout << (*itr)[0].first-szStr << ' ' << (*itr)[0].second-szStr << ' ' << *itr << endl;
results.push_back(std::string((*itr)[group]));
total++;
}
return total;
}
std::string RegexHelper::ReplaceFirst(const char* input,const char* pattern,const char* repValue)
{
//( 1 ) (( 3 ) 2 )(( 5 )4)( 6 )
//(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)?
//^協(xié)議://網(wǎng)址(x.x...x)/路徑(n個(gè)/字串)/網(wǎng)頁(yè)文件(xxx.xxx)
//const char *szReg = "(\\w+)://((\\w+\\.)*\\w+)((/\\w*)*)(/\\w+\\.\\w+)?";
//const char *szStr = "http://www.cppprog.com/2009/0112/48.html";
//repValue = ""
boost::regex reg( pattern , boost::regex::perl|boost::regex::icase);
std::string sret = boost::regex_replace( std::string(input), reg, std::string(repValue));
return sret;
}
std::string RegexHelper::ReplaceAll(const char* input,const char* pattern,const char* repValue)
{
//string s1 = "(<)|(>)|(&)";
//string s2 = "(?1<)(?2>)(?3&)";
boost::regex reg( pattern , boost::regex::perl|boost::regex::icase);
std::string sret = boost::regex_replace( std::string(input), reg, std::string(repValue), boost::match_default | boost::format_all);
return sret;
}
int RegexHelper::Split(const char* input,const char* pattern,std::vector<std::string>& results)
{
boost::regex reg(pattern, boost::regex::perl|boost::regex::icase); //按/符拆分字符串
boost::cregex_token_iterator itrBegin = make_regex_token_iterator(input,reg,-1); //使用-1參數(shù)時(shí)拆分,使用其它數(shù)字時(shí)表示取第幾個(gè)子串,可使用數(shù)組取多個(gè)串
boost::cregex_token_iterator itrEnd;
int total = 0;
for(boost::cregex_token_iterator itr=itrBegin; itr!=itrEnd; ++itr){
//cout << *itr << endl;
results.push_back(std::string(*itr));
total++;
}
return total;
}
int RegexHelper::Split(const char* input,const char* pattern,std::vector<int>& subs,std::vector<std::string>& results)
{
boost::regex reg(pattern, boost::regex::perl|boost::regex::icase); //取/的前一字符和后一字符
boost::cregex_token_iterator itrBegin = make_regex_token_iterator(input,reg,subs); //使用-1參數(shù)時(shí)拆分,使用其它數(shù)字時(shí)表示取第幾個(gè)子串,可使用數(shù)組取多個(gè)串
boost::cregex_token_iterator itrEnd;
int total = 0;
for(boost::cregex_token_iterator itr=itrBegin; itr!=itrEnd; ++itr){
//cout << *itr << endl;
results.push_back(std::string(*itr));
total++;
}
return total;
}
}
測(cè)試代碼
void testregex()
{
//( 1 ) (( 3 ) 2 )(( 5 )4)( 6 )
//(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)?
//^協(xié)議://網(wǎng)址(x.x...x)/路徑(n個(gè)/字串)/網(wǎng)頁(yè)文件(xxx.xxx)
const char *szReg = "(\\w+)://((\\w+\\.)*\\w+)((/\\w*)*)(/\\w+\\.\\w+)?";
const char *szStr = "sss http://www.cppprog.com/2009/0112/48.html";
{ //字符串匹配
cout <<"match:"<< Framework::RegexHelper::IsMatch(szStr,szReg)<<endl;
//assert(r);
}
{ //提取子串
vector<string> results;
int total = Framework::RegexHelper::Match(szStr,szReg,results);
cout << "total="<<total<<endl;
if(total > 0){
for(vector<string>::const_iterator it = results.begin(); it != results.end(); ++it){
cout<< *it <<endl;
}
}
}
{ //查找
cout<<Framework::RegexHelper::Match(szStr,"\\d+")<<endl;
}
{ //替換
cout<<Framework::RegexHelper::ReplaceFirst(szStr,szReg,"ftp://$2$5")<<endl;
}
{ //替換2,把<>&轉(zhuǎn)換成網(wǎng)頁(yè)字符
string s1 = "(<)|(>)|(&)";
string s2 = "(?1<)(?2>)(?3&)";
cout<<Framework::RegexHelper::ReplaceFirst("cout << a&b << endl;",s1.c_str(),s2.c_str())<<endl;
cout<<Framework::RegexHelper::ReplaceAll("cout << a&b << endl;",s1.c_str(),s2.c_str())<<endl;
}
{ //使用迭代器找出所有數(shù)字
vector<string> results;
int total = Framework::RegexHelper::Matches(szStr,"\\d+",results);
cout << "total="<<total<<endl;
if(total > 0){
for(vector<string>::const_iterator it = results.begin(); it != results.end(); ++it){
cout<< *it <<endl;
}
}
}
{ //使用迭代器拆分字符串
vector<string> results;
int total = Framework::RegexHelper::Split(szStr,"/",results);
cout << "total="<<total<<endl;
if(total > 0){
for(vector<string>::const_iterator it = results.begin(); it != results.end(); ++it){
cout<< *it <<endl;
}
}
}
{ //使用迭代器拆分字符串2
vector<string> results;
// 第一子串和第二子串
vector<int> subv;subv.push_back(1),subv.push_back(2);
//取/的前一字符和后一字符
int total = Framework::RegexHelper::Split(szStr,"(.)/(.)",subv,results);
cout << "total="<<total<<endl;
if(total > 0){
for(vector<string>::const_iterator it = results.begin(); it != results.end(); ++it){
cout<< *it <<endl;
}
}
}
}
相關(guān)文章
C語(yǔ)言基礎(chǔ)函數(shù)用法示例詳細(xì)解析
最接地氣的C函數(shù)基礎(chǔ)介紹,此處對(duì)于函數(shù)的相關(guān)知識(shí)點(diǎn)做一些簡(jiǎn)要的介紹,作者實(shí)屬初學(xué),寫(xiě)博客也是作者學(xué)習(xí)的一個(gè)過(guò)程,難免文章中有內(nèi)容理解不到位或者有不當(dāng)之處,還請(qǐng)朋友們不吝指正2021-11-11如何通過(guò)C++求出鏈表中環(huán)的入口結(jié)點(diǎn)
本文主要介紹了通過(guò)C++求解鏈表中環(huán)的入口結(jié)點(diǎn),即給一個(gè)長(zhǎng)度為n鏈表,若其中包含環(huán),請(qǐng)找出該鏈表的環(huán)的入口結(jié)點(diǎn),否則,返回null。需要的朋友可以參考一下2021-12-12深入探索C++中stack和queue的底層實(shí)現(xiàn)
這篇文章主要介紹了C++中的stack和dequeue的底層實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09C++動(dòng)態(tài)調(diào)用動(dòng)態(tài)鏈接庫(kù)(DLL或SO)的方法實(shí)現(xiàn)
動(dòng)態(tài)鏈接庫(kù)是一種Windows操作系統(tǒng)下常見(jiàn)的可執(zhí)行文件格式,它包含了一些可被其他應(yīng)用程序調(diào)用的函數(shù)和數(shù)據(jù),本文主要介紹了C++動(dòng)態(tài)調(diào)用動(dòng)態(tài)鏈接庫(kù)(DLL或SO),感興趣的可以了解一下2024-01-01詳解C 語(yǔ)言項(xiàng)目中.h文件和.c文件的關(guān)系
這篇文章主要介紹了詳解C 語(yǔ)言項(xiàng)目中.h文件和.c文件的關(guān)系的相關(guān)資料,需要的朋友可以參考下2017-05-05C語(yǔ)言函數(shù)調(diào)用基礎(chǔ)應(yīng)用詳解
函數(shù)就是一段封裝好的,可以重復(fù)使用的代碼,它使得我們的程序更加模塊化,不需要編寫(xiě)大量重復(fù)的代碼。這篇文章主要介紹了c語(yǔ)言是如何處理函數(shù)調(diào)用的?需要的朋友可以參考下2023-02-02