最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

封裝常用正則表達式的用法

 更新時間:2014年03月04日 16:20:08   投稿:zxhpj  
這篇文章主要介紹了使用C++封裝常用正則表達式的用法,方便以后直接使用,最后還給出了測試代碼,大家可運行測試使用

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);
    /*
    * 分割字符串并輸出結果
    * @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){
            //       指向子串對應首位置        指向子串對應尾位置          子串內(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&lt;)(?2&gt;)(?3&amp;)";
    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,把<>&轉換成網(wǎng)頁字符
            string s1 = "(<)|(>)|(&)";
            string s2 = "(?1&lt;)(?2&gt;)(?3&amp;)";
            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;
                }
            }
        }
}

相關文章

  • C++中delete函數(shù)的具體使用

    C++中delete函數(shù)的具體使用

    本文主要介紹了C++中delete函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • C語言中字符型數(shù)據(jù)和浮點型數(shù)據(jù)介紹

    C語言中字符型數(shù)據(jù)和浮點型數(shù)據(jù)介紹

    大家好,本篇文章主要講的是C語言中字符型數(shù)據(jù)和浮點型數(shù)據(jù)介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • C++設計模式編程中Template Method模板方法模式的運用

    C++設計模式編程中Template Method模板方法模式的運用

    這篇文章主要介紹了C++設計模式編程中Template Method模板方法模式的運用,講到了包括模板方法模式中的細分方法以及適用場景,需要的朋友可以參考下
    2016-03-03
  • C++類型轉換運算符詳解

    C++類型轉換運算符詳解

    這篇文章主要介紹了C++類型轉換運算符的相關資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • C++多態(tài)虛析構和純虛析構的實現(xiàn)

    C++多態(tài)虛析構和純虛析構的實現(xiàn)

    本文主要介紹了C++多態(tài)虛析構和純虛析構的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • C++?opencv圖像處理實現(xiàn)灰度變換示例

    C++?opencv圖像處理實現(xiàn)灰度變換示例

    這篇文章主要為大家介紹了C++?opencv圖像處理灰度變換的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • C語言單循環(huán)鏈表的表示與實現(xiàn)實例詳解

    C語言單循環(huán)鏈表的表示與實現(xiàn)實例詳解

    這篇文章主要介紹了C語言單循環(huán)鏈表的表示與實現(xiàn),對于學習數(shù)據(jù)結構與算法的朋友來說很有參考借鑒價值,需要的朋友可以參考下
    2014-07-07
  • C++實現(xiàn)讀取特定路徑下文件夾及文件名的方法

    C++實現(xiàn)讀取特定路徑下文件夾及文件名的方法

    這篇文章主要介紹了C++實現(xiàn)讀取特定路徑下文件夾及文件名的方法,需要的朋友可以參考下
    2014-07-07
  • C#中?MessageBox的使用技巧

    C#中?MessageBox的使用技巧

    這篇文章主要介紹了C#中?MessageBox的使用技巧,在C#中MessageBox消息對話框位于System.Windows.Forms命名空間中,更多詳細的內(nèi)容需要的朋友可以參考一下
    2022-08-08
  • C語言直接插入排序算法

    C語言直接插入排序算法

    大家好,本篇文章主要講的是C語言直接插入排序算法,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01

最新評論

铜鼓县| 岢岚县| 东兰县| 诸城市| 邵阳市| 井研县| 那坡县| 永年县| 西乡县| 宁安市| 若尔盖县| 图片| 若尔盖县| 民和| 通河县| 织金县| 积石山| 松滋市| 屏山县| 彭山县| 清镇市| 林甸县| 佳木斯市| 阳西县| 马关县| 丽江市| 嘉荫县| 五台县| 博客| 兴城市| 安吉县| 韶山市| 高陵县| 灌阳县| 海阳市| 大丰市| 乡宁县| 阿坝县| 桂林市| 阳春市| 正定县|