封裝常用正則表達式的用法
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 輸出的字符串數(shù)組
*/
static int Match(const char* input,const char* pattern,std::vector<std::string>& results);
/*
* 匹配字符串數(shù)目
* @param: input 輸入字符串
* @param:pattern 正則表達式
*/
static int Matches(const char* input,const char* pattern);
/*
* 輸出所有匹配字符串或其捕獲組
* @param: input 輸入字符串
* @param:pattern 正則表達式
* @param: results 輸出的字符串數(shù)組
* @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 ReplaceFirst(const char* input,const char* pattern,const char* repValue);
/*
* 替換所有匹配字符串
* @param: input 輸入字符串
* @param:pattern 正則表達式
* @param:repValue 被替換值,可以是捕獲組的組合
*/
static std::string ReplaceAll(const char* input,const char* pattern,const char* repValue);
/*
* 分割字符串并輸出結(jié)果
* @param: input 輸入字符串
* @param:pattern 正則表達式
* @param: results 輸出的字符串數(shù)組
*/
static int Split(const char* input,const char* pattern,std::vector<std::string>& results);
/*
* 分割字符串并根據(jù)捕獲組輸出
* @param: input 輸入字符串
* @param:pattern 正則表達式
* @param:subs 捕獲組
* @param: results 輸出的字符串數(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){
// 指向子串對應(yīng)首位置 指向子串對應(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個/字串)/網(wǎng)頁文件(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ù)組取多個串
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ù)組取多個串
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+)?
//^協(xié)議://網(wǎng)址(x.x...x)/路徑(n個/字串)/網(wǎng)頁文件(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)頁字符
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;
}
}
}
}
您可能感興趣的文章
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 01-10C語言中字符串常用函數(shù)strcat與strcpy的用法介紹
- 01-10深入解析C++ STL中的常用容器
- 01-10STL常用容器詳細解析
- 01-10pcre函數(shù)詳細解析
- 01-10淺析STL中的常用算法
- 01-10c++中的string常用函數(shù)用法總結(jié)
- 01-10php正則表達式的基本語法總結(jié)
- 01-10C++內(nèi)核對象封裝單實例啟動程序的類
- 01-10常用排序算法整理分享(快速排序算法、希爾排序)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載