爲了儘快的掌握DEELX中正則表達是的 應用,下面寫了一個小程序(我一直相信,帶着問題去學習總會更大程度的激發我們的能動性,加深我們對知識的理解)。
下面這個實例程序主要用於匹配一串代碼中的("|" "->" "=" "==" "+" "-" )串,其中應當注意的是“==”和“=”,應該把"=="正則表達式優先獲得匹配.
不多解釋了,上代碼.
#include <iostream>
#include "deelx.h"
using namespace std;
int main()
{
string rex="(==)|((?[^=!+-*])=)|(->)|-|+|(\\|)";
string math="int a=12; if (a==b) return p->x;";
static CRegexpT <char> regexp(rex.c_str());
CContext * pContext = regexp.PrepareMatch(math.c_str());
MatchResult result=regexp.Match(pContext);
while(result.IsMatched())
{
int start=result.GetStart();
int end=result.GetEnd();
cout<<math.substr(start,end-start)<<endl;
result=regexp.Match(pContext);
}
regexp.ReleaseContext(pContext);
return 0;
}
=
==
->