#include <string>
#include "regexpr2.h"
using namespace std;
using namespace regex;//greta库的命名空间
//若链接出错,设置MFC静态链接
//查找匹配串
//返回结果匹配串(CStringArray数组指针类型)
CStringArray* Find(LPCTSTR m_reg,LPCTSTR m_source,REGEX_FLAGS dwStyle = NOCASE | MULTILINE)
{
CStringArray* strArray = new CStringArray();
REGEX_FLAGS dw = GLOBAL | ALLBACKREFS | dwStyle;
rpattern reg(m_reg, dw);
match_results results;
match_results::backref_type bt = reg.match(m_source, results);
int iGroups = reg.cgroups();
int nCount = 0;
if(bt.matched)
{
for(int i=0; i<results.cbackrefs(); i++)
{
if(i%iGroups == 0)
{
nCount++;
strArray->Add(results.backref(i).str().c_str());
}
}
}
return strArray;
}
//查找匹配串
//返回结果匹配串(CStringArray数组指针类型)
CStringArray* Find2(LPCTSTR m_reg,LPCTSTR m_source,REGEX_FLAGS dwStyle = NOCASE | MULTILINE)
{
CStringArray* strArray = new CStringArray();
REGEX_FLAGS dw = GLOBAL | ALLBACKREFS | dwStyle;
rpattern reg(m_reg, dw);
match_results results;
match_results::backref_type bt = reg.match(m_source, results);
if(bt.matched)
{
match_results::backref_vector vec = results.all_backrefs();
match_results::backref_vector::iterator iter;
for(iter = vec.begin(); iter != vec.end(); iter++)
{
string str = (*iter).str();
strArray->Add(str.c_str());
}
}
return strArray;
}
//使用实例
CStringArray* str = Find(m_reg, m_source);
for(int i=0; i< str->GetSize(); i++)
{
AfxMessageBox((*str)[i]);
}
//替换匹配串
//返回结果替换后字符串(CString类型)
CString Sub(LPCTSTR m_reg,LPCTSTR m_sub, LPCTSTR m_source,REGEX_FLAGS dwStyle = NOCASE | MULTILINE)
{
CString lpSub;
REGEX_FLAGS dw = GLOBAL | ALLBACKREFS | dwStyle;
rpattern reg(m_reg, m_sub, dw);
subst_results results;
string str(m_source);
int nCount = reg.substitute(str, results);
lpSub = str.c_str();
return lpSub;
}
//使用实例
CString str = Sub(m_reg, m_sub, m_source);
AfxMessageBox(str);
//分割串
//返回结果分割后子串(CStringArray数组类型)
CStringArray* Split(LPCTSTR m_reg, LPCTSTR m_source,REGEX_FLAGS dwStyle = NOCASE | MULTILINE)
{
CStringArray* strArray = new CStringArray();
REGEX_FLAGS dw = GLOBAL | ALLBACKREFS | dwStyle;
rpattern reg(m_reg, dw);
split_results results;
string str(m_source);
int nCount = reg.split(str, results);
for(int i=0; i<nCount; i++)
{
string split = results[i];
strArray->Add(split.c_str());
}
return strArray;
}
//使用实例
CStringArray* str = Split(m_reg, m_source);
for(int i=0; i<str->GetSize(); i++)
{
AfxMessageBox((*str)[i]);
}
VC6微软正则表达式greta使用案例,码迷,mamicode.com
原文地址:http://www.cnblogs.com/doudongchun/p/3699650.html