关于greta正则表达式的使用收藏
以上的这些是经过我一上午的研究,关于split_results 还不会使用,希望有人指点.
新一篇: WebBrowser2封装的容器 |
<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>VC可以使用的正则表达式我知道的有:ATL CAtlRegExp,GRETA,Boost::regex 等正则表达式库,这些表达式库使我们可以方便地利用正则库的巨大威力,给我们的工作提供了便利。
这种些正则表达式各自有自己的优点,但我最终还是选择了使用GRETA正则表达式,因为ATL CAtlRegExp速度相比较有些慢,而Boost库我目前还不会使用.GRETA相比较Boost是轻量级的,而且也是转门用来处理正则表达式的.速度也是最快的.
但是目前在网上有关GRETA库使用的文档真是少之有少,由于此库是完全天源的,所以下载下来以后只有6个源文件,使用这些源文件可以两种方法, 1:只接添加它们到你的工程.2:编译成为静态库.第1种方式不是太好,会使你的工程里有过多的类而显得不清晰.在网上可以找到一些已经编译好的Lib 库,最好不要使用,因为Lib库和你工程的run-time library不一样,会导致链接错误.所以最好自己生成和工程同样的run-time library.
使用时只需要包含regexpr2.h即可,有关正则表达式的语法可以参见其它方档.
我们只需要关注GRETA的的几个对象即可,它们是:
- rpattern : 搜索的模式
- match_results : 放置匹配的容器
- subst_results : 放置替换的容器
- split_results : 放置分离的窗口(此接口的使用方法不是很清楚)
使用方法参见下面的实例:
void
CPrepareHtml::FindLable(CString strContant)
{
rpattern pattern("/[LABEL +id *= *"([0-9A-Za-z_-]+)" */]",GLOBAL|NOCASE|ALLBACKREFS);
match_results results;
match_results::backref_type br = pattern.match((LPCTSTR)strContant,results);
// match_results::backref_vector backVec = results.all_backrefs();
IXMLDOMDocument2Ptr pDoc;
IXMLDOMElementPtr pElem;
HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument));
if (SUCCEEDED(hr))
{
pElem = pDoc->createElement("LABELS");
pElem = pDoc->appendChild(pElem);
for (int iter = 1;iter < results.cbackrefs();iter += 2) // 目前还没有找到读取子表达式更好的方法
{
match_results::backref_type br_Temp = results.backref(iter);
std::string strValue = br_Temp.str();
IXMLDOMElementPtr pElemLable = pDoc->createElement("LABEL");
pElemLable = pElem->appendChild(pElemLable);
pElemLable->Puttext(strValue.c_str()); // 匹配到的字符串生成XML结点
}
/*
for (int iter = 1;iter < backVec.size();iter += 2)
{
match_results::backref_type br_Temp = backVec[iter];
std::string strValue = br_Temp.str();
IXMLDOMElementPtr pElemLable = pDoc->createElement("LABEL");
pElemLable = pElem->appendChild(pElemLable);
pElemLable->Puttext(strValue.c_str());
}
*/
}
static const rpattern patsubst("/[APP +id *= *"HTMLROOT *" */]","C://whiz",GLOBAL|NOCASE); // 第二个参数是表达式语法
subst_results subResults;
std::string sContant(strContant);
int nSub = patsubst.substitute(sContant,subResults);
sContant = ReplaceLable(pDoc,sContant);
WriteHTMLFile("C:/temp/tempHTML.html",sContant.c_str(),sContant.length());
}
std:: string CPrepareHtml::ReplaceLable(IXMLDOMDocument2Ptr & pDoc,std:: string strContant)
{
CString strPattern;
IXMLDOMNodePtr pLable = NULL;
IXMLDOMNodeListPtr pLableList = pDoc->selectNodes("/LABELS/LABEL"); // 读取XML结点
while (pLable = pLableList->nextNode())
{
CString strLable = (BSTR)pLable->Gettext();
CString strValue = m_IniDic[strLable];
strPattern.Format("/[LABEL *id="%s" */]",strLable);
strContant = ReplaceTag(strPattern,strValue,strContant);
}
return strContant;
}
std:: string CPrepareHtml::ReplaceTag(LPCTSTR pattern,LPCTSTR value,std:: string & Contant)
{
rpattern patsubst(pattern,value,GLOBAL|NOCASE);
subst_results subResults;
int nSub = patsubst.substitute(Contant,subResults); // 替换
return Contant;
}
{
rpattern pattern("/[LABEL +id *= *"([0-9A-Za-z_-]+)" */]",GLOBAL|NOCASE|ALLBACKREFS);
match_results results;
match_results::backref_type br = pattern.match((LPCTSTR)strContant,results);
// match_results::backref_vector backVec = results.all_backrefs();
IXMLDOMDocument2Ptr pDoc;
IXMLDOMElementPtr pElem;
HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument));
if (SUCCEEDED(hr))
{
pElem = pDoc->createElement("LABELS");
pElem = pDoc->appendChild(pElem);
for (int iter = 1;iter < results.cbackrefs();iter += 2) // 目前还没有找到读取子表达式更好的方法
{
match_results::backref_type br_Temp = results.backref(iter);
std::string strValue = br_Temp.str();
IXMLDOMElementPtr pElemLable = pDoc->createElement("LABEL");
pElemLable = pElem->appendChild(pElemLable);
pElemLable->Puttext(strValue.c_str()); // 匹配到的字符串生成XML结点
}
/*
for (int iter = 1;iter < backVec.size();iter += 2)
{
match_results::backref_type br_Temp = backVec[iter];
std::string strValue = br_Temp.str();
IXMLDOMElementPtr pElemLable = pDoc->createElement("LABEL");
pElemLable = pElem->appendChild(pElemLable);
pElemLable->Puttext(strValue.c_str());
}
*/
}
static const rpattern patsubst("/[APP +id *= *"HTMLROOT *" */]","C://whiz",GLOBAL|NOCASE); // 第二个参数是表达式语法
subst_results subResults;
std::string sContant(strContant);
int nSub = patsubst.substitute(sContant,subResults);
sContant = ReplaceLable(pDoc,sContant);
WriteHTMLFile("C:/temp/tempHTML.html",sContant.c_str(),sContant.length());
}
std:: string CPrepareHtml::ReplaceLable(IXMLDOMDocument2Ptr & pDoc,std:: string strContant)
{
CString strPattern;
IXMLDOMNodePtr pLable = NULL;
IXMLDOMNodeListPtr pLableList = pDoc->selectNodes("/LABELS/LABEL"); // 读取XML结点
while (pLable = pLableList->nextNode())
{
CString strLable = (BSTR)pLable->Gettext();
CString strValue = m_IniDic[strLable];
strPattern.Format("/[LABEL *id="%s" */]",strLable);
strContant = ReplaceTag(strPattern,strValue,strContant);
}
return strContant;
}
std:: string CPrepareHtml::ReplaceTag(LPCTSTR pattern,LPCTSTR value,std:: string & Contant)
{
rpattern patsubst(pattern,value,GLOBAL|NOCASE);
subst_results subResults;
int nSub = patsubst.substitute(Contant,subResults); // 替换
return Contant;
}
发表于 @ 2008年02月18日 09:41:00|评论(3<script type="text/javascript">AddFeedbackCountStack("2102172")</script>)|编辑
评论
# fietiger 发表于2008-07-19 19:25:19 IP: 59.62.128.*-
你好,我在使用Greta的时候参考了你的代码,其中的替换竟然会出问题。我的代码是这样的:
static const rpattern s_crlf( "(?<!/r)/n", "/r/n", GLOBAL );
string str ((LPCTSTR)"Wer.exe");
s_crlf.substitute( str, results );
就这样一加最后一句就出现:
error C2587: '_U' :
等错误,我怀疑是不是编译的设置问题,麻烦请教一下。谢谢!
# fietiger 发表于2008-07-19 19:29:23 IP: 59.62.128.*-
另外,我使用分隔与查找都可以的。只是这个替换不可以用。
split_results sr;
const char * m_Pattern = "[/]+";
rpattern pat((LPCTSTR)m_Pattern);
split_results::iterator iter;
pat.split((LPCTSTR)strPath, sr);
CString sSplitResult;
iter = sr.begin();
sSplitResult += CString(iter->begin(), iter->size());
sSplitResult += "/";
if(_taccess(sSplitResult, 0)!=0)
{
// MessageBox("盘符错误!");
/*CreateDirectory(m_strPath, NULL);*/
return false;
}
# sunmz_wjxy 发表于2008-07-21 11:20:13 IP: 219.140.59.*-
如果greta是动态库或者静态库,则要和程序同样的编译选项编译才行
run-time library一定要一样才可以