Encapsulates the use of common regular expressions

  • 2020-04-02 02:10:31
  • OfStack

Regexhelper. H


#ifndef REGEX_HELPER_H_INCLUDE
#define REGEX_HELPER_H_INCLUDE
#include<string>
#include<vector>
namespace Framework{
class RegexHelper
{
public:
    RegexHelper();
    virtual ~RegexHelper();
    
    static bool IsMatch(const char* input,const char* pattern);
    
    static std::string Match(const char* input,const char* pattern,int group = 0);
    
    static int Match(const char* input,const char* pattern,std::vector<std::string>& results);
    
    static int Matches(const char* input,const char* pattern);
    
    static int Matches(const char* input,const char* pattern,std::vector<std::string>& results,int group = 0);
    
    static std::string ReplaceFirst(const char* input,const char* pattern,const char* repValue);
    
    static std::string ReplaceAll(const char* input,const char* pattern,const char* repValue);
    
    static int Split(const char* input,const char* pattern,std::vector<std::string>& 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){ //If the match is successful
        //cout << "match success" << endl;
        //Displays all substrings
        for(boost::cmatch::iterator itr=mat.begin(); itr!=mat.end(); ++itr){
            //& have spent & have spent & have spent & have spent & have spent & have spent The pointing substring corresponds to the leading position & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent Pointing to substring corresponding to tail position & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent Content of the substring
            //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);    //Find the number in the string
    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);    //Find the number in the string
    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+)?
    //^ agreement :// The url (x.x...x)/ The path (n a / string )/ Web page file (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);  //Split the string by /
    boost::cregex_token_iterator itrBegin = make_regex_token_iterator(input,reg,-1); //Split when -1 parameter is used, which substring is taken when other Numbers are used, and multiple strings can be taken by array
    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);  //Take the first and last characters of/(this string image seems to be a bit evil -_-)
    boost::cregex_token_iterator itrBegin = make_regex_token_iterator(input,reg,subs); //Split when -1 parameter is used, which substring is taken when other Numbers are used, and multiple strings can be taken by array
    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;
}
}

The test code


void testregex()
{
     //( 1 )   ((  3  )  2 )((  5 )4)(    6    )
        //(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)?
        //^ agreement :// The url (x.x...x)/ The path (n a / string )/ Web page file (xxx.xxx)
        const char *szReg = "(\w+)://((\w+\.)*\w+)((/\w*)*)(/\w+\.\w+)?";
        const char *szStr = "sss http://www.cppprog.com/2009/0112/48.html";
        {    //String matching
            cout <<"match:"<< Framework::RegexHelper::IsMatch(szStr,szReg)<<endl;
            //assert(r);
        }
        {    //Extract the substring
            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;
                }
            }
        }
        { //To find the
            cout<<Framework::RegexHelper::Match(szStr,"\d+")<<endl;
        }
        { //replace
            cout<<Framework::RegexHelper::ReplaceFirst(szStr,szReg,"ftp://$2$5")<<endl;
        }
        { //replace2 , <>& Convert to web page characters 
            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;
        }
        { //Use the iterator to find all the Numbers
            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;
                }
            }
        }
        { //Use iterators to split strings
            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;
                }
            }
        }
        { //Use iterators to split strings2
            vector<string> results;
            //The first substring and the second substring
            vector<int> subv;subv.push_back(1),subv.push_back(2);
            //Take the first and last characters of/(this string image seems to be a bit evil -_-)
            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;
                }
            }
        }
}


Related articles: