亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C > 正文

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

2020-01-26 15:35:20
字體:
來源:轉載
供稿:網友

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 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 輸出的字符串數組
    */
    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::ReplaceFirst(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::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參數時拆分,使用其它數字時表示取第幾個子串,可使用數組取多個串
    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,"http://d+")<<endl;

        }

        { //替換
            cout<<Framework::RegexHelper::ReplaceFirst(szStr,szReg,"ftp://$2$5")<<endl;
        }
        { //替換2,把<>&轉換成網頁字符
            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;

        }

        { //使用迭代器找出所有數字
            vector<string> results;
            int total = Framework::RegexHelper::Matches(szStr,"http://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;
                }
            }
        }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美精品久久久久a| 亚洲欧美激情一区| 欧美午夜激情在线| 亚洲va欧美va在线观看| 久久精品国产欧美激情| 国产精品久久久久久久久久久新郎| 日韩小视频网址| 欧美日韩亚洲一区二区三区| 日韩亚洲国产中文字幕| 亚洲欧美制服第一页| 国外成人免费在线播放| 久久99国产精品久久久久久久久| 色偷偷av亚洲男人的天堂| 自拍偷拍亚洲精品| 3344国产精品免费看| 国产精品日韩精品| 欧美小视频在线观看| 亚洲一区二区在线播放| 亚洲成人激情视频| 成人国产在线视频| 久久久久久香蕉网| 日韩欧美在线观看视频| 色综合视频网站| 成人国产在线激情| 亚洲黄色片网站| 午夜精品国产精品大乳美女| 欧美精品videosex极品1| 国产精品久久久久影院日本| 久久久久久国产精品| 国产精品久久久久久av下载红粉| 青青草原成人在线视频| 性金发美女69hd大尺寸| 国产精品成人av在线| 亚洲成年人在线| 不卡av在线播放| 伊人成人开心激情综合网| 欧美视频在线免费| 九九综合九九综合| 欧美激情在线有限公司| 国产盗摄xxxx视频xxx69| 欧美日韩国产成人在线| 亚洲欧美制服丝袜| 日韩av综合中文字幕| 国产亚洲日本欧美韩国| 日韩美女在线观看一区| 欧美极品少妇xxxxⅹ免费视频| 欧美老少做受xxxx高潮| 国产精品久久久久久久av电影| 亚洲精品一区在线观看香蕉| 久久精品欧美视频| 国产精品夫妻激情| 一区二区欧美日韩视频| 成人午夜在线观看| 1769国内精品视频在线播放| 国产成人精品综合| 欧美激情在线播放| 成人性生交大片免费看视频直播| 欧美成人sm免费视频| 欧美日韩亚洲一区二区三区| 91免费精品国偷自产在线| 国产欧美一区二区三区久久人妖| 国产精品视频免费在线| 欧美精品在线免费| 欧美视频中文在线看| 国产欧美 在线欧美| 亚洲第一国产精品| 国产有码一区二区| www.国产一区| 欧美激情欧美激情| 欧洲成人午夜免费大片| 成人黄色在线观看| 欧美日韩在线视频一区二区| 国产精品96久久久久久又黄又硬| 久久久视频免费观看| 久久伊人精品天天| 欧美激情精品久久久久久变态| 78色国产精品| 国产一区二区动漫| 欧美在线视频免费播放| 日韩国产高清污视频在线观看| 国产欧美精品日韩精品| 欧美国产亚洲精品久久久8v| 亚洲一区精品电影| 亚洲夜晚福利在线观看| 91精品久久久久久久久久久久久| 精品久久久久久中文字幕| 69国产精品成人在线播放| 国产精品视频久久| 98视频在线噜噜噜国产| 欧美香蕉大胸在线视频观看| 国产性猛交xxxx免费看久久| 久久亚洲电影天堂| 国产成人91久久精品| 亚洲精品一二区| 欧美在线xxx| 欧美激情精品在线| 欧美日韩不卡合集视频| 亚洲精品99久久久久中文字幕| 国产精品自产拍高潮在线观看| 久久久久在线观看| 久久69精品久久久久久国产越南| 欧美黑人极品猛少妇色xxxxx| 视频直播国产精品| 亚洲影院在线看| 国产综合福利在线| 91国产中文字幕| 欧美国产日韩二区| 亚洲欧美色图片| 亚洲已满18点击进入在线看片| 欧美午夜精品在线| 51视频国产精品一区二区| 亚洲第一精品福利| 精品久久久久久| 岛国视频午夜一区免费在线观看| 欧美激情亚洲精品| 久久久日本电影| 亚洲国产欧美日韩精品| 久久亚洲私人国产精品va| 亚洲欧美国产高清va在线播| 国产欧美日韩亚洲精品| 精品激情国产视频| 久久影视三级福利片| 96国产粉嫩美女| 国产精品久久久久久久久久99| 日韩av在线影院| www.99久久热国产日韩欧美.com| 日韩av手机在线观看| 91在线视频成人| 7777kkkk成人观看| 91日韩在线播放| 精品国产91乱高清在线观看| 色多多国产成人永久免费网站| 亚洲性生活视频在线观看| 欧洲美女7788成人免费视频| 国产欧美一区二区三区视频| 91av福利视频| 国产精品久久综合av爱欲tv| 91精品国产91久久久| 欧美成人精品在线| 51视频国产精品一区二区| 欧美激情亚洲自拍| 欧美二区乱c黑人| 国产精品免费久久久久影院| 欧美一级片久久久久久久| 亚洲天堂av电影| 51视频国产精品一区二区| 精品久久久国产| 国产精品草莓在线免费观看| 亚洲国产欧美一区二区丝袜黑人| 色多多国产成人永久免费网站| 亚洲精品欧美日韩| 欧美精品免费在线| 91精品国产亚洲| 中文字幕精品久久| 91网站免费观看| 国产一区二区在线免费| 欧美日韩美女视频| 亚洲成年网站在线观看| 久久精品国产精品| 成人国产亚洲精品a区天堂华泰| 亚洲一区二区三区乱码aⅴ| 久久精品视频在线播放| 91禁国产网站| 国产精品午夜一区二区欲梦|