正则中的贪婪与非贪婪性能之差的原因....做个笔记...
非贪婪的算法()对*的处理
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;
}
可以看出, 在贪婪的算法中, 事先读取到了所有的符合当前规则的字符, 然后回溯直到找到与剩下的规则匹配的位置