regexhelper.h
protected:
private:
};
}
#endif // REGEX_HELPER_H_INCLUDE
</div>
regexhelper.cpp
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::creg