天天看點

VC6微軟正規表達式greta使用案例

#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* find2(lpctstr m_reg,lpctstr m_source,regex_flags dwstyle = nocase | multiline) 

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());

//使用執行個體

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;

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) 

split_results results;

int ncount = reg.split(str, results);

for(int i=0; i<ncount; i++)

string split = results[i];

strarray->add(split.c_str());

cstringarray* str = split(m_reg, m_source);

for(int i=0; i<str->getsize(); i++)