Regexhelper.h
Copy Code code as follows:
#ifndef Regex_helper_h_include
#define Regex_helper_h_include
#include <string>
#include <vector>
Namespace framework{
Class Regexhelper
{
Public
Regexhelper ();
Virtual ~regexhelper ();
/*
* Whether to include a matching string
* @param: Input input string
* @param: Pattern Regular Expressions
*/
static bool IsMatch (const char* input,const char* pattern);
/*
* Gets the first matching string or its strings
* @param: Input input string
* @param: Pattern Regular Expressions
* @param: Group Child capture groups
*/
Static std::string Match (const char* input,const char* Pattern,int group = 0);
/*
* Get the first matching string for all capturing groups
* @param: Input input string
* @param: Pattern Regular Expressions
* @param: Results Output string array
*/
static int Match (const char* input,const char* pattern,std::vector<std::string>& results);
/*
* Number of matching strings
* @param: Input input string
* @param: Pattern Regular Expressions
*/
static int matches (const char* input,const char* pattern);
/*
* Output all matching strings or their capturing groups
* @param: Input input string
* @param: Pattern Regular Expressions
* @param: Results Output string array
* @param: Group capture groups
*/
static int matches (const char* input,const char* pattern,std::vector<std::string>& results,int group = 0);
/*
* Replace the first matching string
* @param: Input input string
* @param: Pattern Regular Expressions
* @param: Repvalue is replaced with a value that can be a combination of capturing groups
*/
Static std::string Replacefirst (const char* input,const char* pattern,const);
/*
* Replace all matching strings
* @param: Input input string
* @param: Pattern Regular Expressions
* @param: Repvalue is replaced with a value that can be a combination of capturing groups
*/
Static std::string replaceall (const char* input,const char* pattern,const);
/*
* Split the string and output the result
* @param: Input input string
* @param: Pattern Regular Expressions
* @param: Results Output string array
*/
static int Split (const char* input,const char* pattern,std::vector<std::string>& results);
/*
* Split string and output according to capturing group
* @param: Input input string
* @param: Pattern Regular Expressions
* @param: Subs capture Group
* @param: Results Output string array
*/
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
Copy Code code as follows:
#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::p erl|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::p erl|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::p erl|boost::regex::icase);
BOOL Success =boost::regex_search (input, Mat, Reg);
int total = 0;
if (success) {//if the match succeeds
cout << "Match success" << Endl;
Show All substrings
For (Boost::cmatch::iterator Itr=mat.begin (); Itr!=mat.end (); ++itr) {
Point to substring corresponding to first position point substring corresponding to tail position substring contents
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::p erl|boost::regex::icase); Find a number in a 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 << End L
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::p erl|boost::regex::icase); Find a number in a 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 << End L
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://URL (x.x ...) x)/path (n/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::p erl|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::p erl|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::p erl|boost::regex::icase); Split string by/character
Boost::cregex_token_iterator Itrbegin = Make_regex_token_iterator (input,reg,-1); Split when using the 1 parameter, use other numbers to indicate the number of substrings, use an array to fetch multiple strings
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>&:: string>& results)
{
Boost::regex reg (Pattern, Boost::regex::p erl|boost::regex::icase); The previous and last characters (this string image looks a bit sinister-_-)
Boost::cregex_token_iterator Itrbegin = Make_regex_token_iterator (input,reg,subs); Split when using the 1 parameter, use other numbers to indicate the number of substrings, use an array to fetch multiple strings
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;
}
}
Test code
Copy Code code as follows:
void Testregex ()
{
(1) ((3) 2) ((5) 4) (6)
(/w+)://(/w+/.) */w+) ((//w*) *) (//w+/./w+)?
^ agreement://URL (x.x ...) x)/path (n/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 match
cout << "match:" << Framework::regexhelper::ismatch (Szstr,szreg) <<endl;
ASSERT (R);
}
{//Extract 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;
}
}
}
{//Find
Cout<<framework::regexhelper::match (Szstr, "\\d+") <<endl;
}
{//replace
Cout<<framework::regexhelper::replacefirst (Szstr,szreg, "ftp://$2$5") <<endl;
}
{//replace 2, convert <>& to 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 iterators to find all 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;
}
}
}
{///using 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 iterator to split string 2
vector<string> results;
First substring and second substring
Vector<int> Subv;subv.push_back (1), Subv.push_back (2);
The previous and last characters (this string image looks a bit sinister-_-)
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;
}
}
}
}