封裝經常使用正則表達式的用法。本站提示廣大學習愛好者:(封裝經常使用正則表達式的用法)文章只能為提供參考,不一定能成為您想要的結果。以下是封裝經常使用正則表達式的用法正文
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 正則表達式
*/
static bool IsMatch(const char* input,const char* pattern);
/*
* 獲得首個婚配字符串或其字串
* @param: input 輸出字符串
* @param:pattern 正則表達式
* @param:group 子捕捉組
*/
static std::string Match(const char* input,const char* pattern,int group = 0);
/*
* 獲得首個婚配字符串一切捕捉組
* @param: input 輸出字符串
* @param:pattern 正則表達式
* @param: results 輸入的字符串數組
*/
static int Match(const char* input,const char* pattern,std::vector<std::string>& results);
/*
* 婚配字符串數量
* @param: input 輸出字符串
* @param:pattern 正則表達式
*/
static int Matches(const char* input,const char* pattern);
/*
* 輸入一切婚配字符串或其捕捉組
* @param: input 輸出字符串
* @param:pattern 正則表達式
* @param: results 輸入的字符串數組
* @param:group 捕捉組
*/
static int Matches(const char* input,const char* pattern,std::vector<std::string>& results,int group = 0);
/*
* 調換首個婚配字符串
* @param: input 輸出字符串
* @param:pattern 正則表達式
* @param:repValue 被調換值,可所以捕捉組的組合
*/
static std::string WordStrFirst(const char* input,const char* pattern,const char* repValue);
/*
* 調換一切婚配字符串
* @param: input 輸出字符串
* @param:pattern 正則表達式
* @param:repValue 被調換值,可所以捕捉組的組合
*/
static std::string WordStrAll(const char* input,const char* pattern,const char* repValue);
/*
* 朋分字符串並輸入成果
* @param: input 輸出字符串
* @param:pattern 正則表達式
* @param: results 輸入的字符串數組
*/
static int Split(const char* input,const char* pattern,std::vector<std::string>& results);
/*
* 朋分字符串並依據捕捉組輸入
* @param: input 輸出字符串
* @param:pattern 正則表達式
* @param:subs 捕捉組
* @param: results 輸入的字符串數組
*/
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){
// 指向子串對應首地位 指向子串對應尾地位 子串內容
//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); //查找字符串裡的數字
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); //查找字符串裡的數字
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::WordStrFirst(const char* input,const char* pattern,const char* repValue)
{
//( 1 ) (( 3 ) 2 )(( 5 )4)( 6 )
//(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)?
//^協定://網址(x.x...x)/途徑(n個/字串)/網頁文件(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::WordStrAll(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參數時拆分,應用其它數字時表現取第幾個子串,可以使用數組取多個串
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參數時拆分,應用其它數字時表現取第幾個子串,可以使用數組取多個串
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;
}
}
測試代碼
void testregex()
{
//( 1 ) (( 3 ) 2 )(( 5 )4)( 6 )
//(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)?
//^協定://網址(x.x...x)/途徑(n個/字串)/網頁文件(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::WordStrFirst(szStr,szReg,"ftp://$2$5")<<endl;
}
{ //調換2,把<>&轉換成網頁字符
string s1 = "(<)|(>)|(&)";
string s2 = "(?1<)(?2>)(?3&)";
cout<<Framework::RegexHelper::WordStrFirst("cout << a&b << endl;",s1.c_str(),s2.c_str())<<endl;
cout<<Framework::RegexHelper::WordStrAll("cout << a&b << endl;",s1.c_str(),s2.c_str())<<endl;
}
{ //應用迭代器找出一切數字
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;
}
}
}
}