为了尽快的掌握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;
- }
= == ->