`
yuanlanxiaup
  • 浏览: 858356 次
文章分类
社区版块
存档分类
最新评论

正则表达式中的贪婪与非贪婪的性能问题

 
阅读更多

正则中的贪婪与非贪婪性能之差的原因....做个笔记...

非贪婪的算法()对*的处理

int matchstar(int c, char *reg, char *text) {

do {

if(matchhere(reg, text)) return 1;

} while(*text != '/0' && (*text ++ == c || c == '.'));

return 0;

}

贪婪的算法对*的处理

int matchstar(int c, char *reg, char *text) {

char *t;

for(t = text; *t != '/0' && (*t == c || c == '.'); t ++) ;

do {

if(matchhere(reg, t)) return 1;

} while(t-- > text);

return 0;

}

可以看出, 在贪婪的算法中, 事先读取到了所有的符合当前规则的字符, 然后回溯直到找到与剩下的规则匹配的位置

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics