comparison lispref/searching.texi @ 55736:c5ea37f370b5

(Regexp Special): Nested repetition can be infloop.
author Richard M. Stallman <rms@gnu.org>
date Sat, 22 May 2004 22:05:57 +0000
parents bb6e41200945
children a75eb408147c 4c90ffeb71c5
comparison
equal deleted inserted replaced
55735:1874eee23678 55736:c5ea37f370b5
242 first tries to match all three @samp{a}s; but the rest of the pattern is 242 first tries to match all three @samp{a}s; but the rest of the pattern is
243 @samp{ar} and there is only @samp{r} left to match, so this try fails. 243 @samp{ar} and there is only @samp{r} left to match, so this try fails.
244 The next alternative is for @samp{a*} to match only two @samp{a}s. With 244 The next alternative is for @samp{a*} to match only two @samp{a}s. With
245 this choice, the rest of the regexp matches successfully.@refill 245 this choice, the rest of the regexp matches successfully.@refill
246 246
247 Nested repetition operators can be extremely slow if they specify 247 Nested repetition operators can be extremely slow or loop infinitely
248 backtracking loops. For example, it could take hours for the regular 248 if they use repetition operators inside repetition operators. For
249 expression @samp{\(x+y*\)*a} to try to match the sequence 249 example, it could take hours for the regular expression
250 @samp{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz}, before it ultimately fails. 250 @samp{\(x+y*\)*a} to try to match the sequence
251 The slowness is because Emacs must try each imaginable way of grouping 251 @samp{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz}, before it ultimately
252 the 35 @samp{x}s before concluding that none of them can work. To make 252 fails. Emacs must try each way of grouping the 35 @samp{x}s before
253 sure your regular expressions run fast, check nested repetitions 253 concluding that none of them can work. Even worse, @samp{\(x*\)*} can
254 match the null string in infinitely many ways, so it causes an
255 infinite loop. To avoid these problems, check nested repetitions
254 carefully. 256 carefully.
255 257
256 @item @samp{+} 258 @item @samp{+}
257 @cindex @samp{+} in regexp 259 @cindex @samp{+} in regexp
258 is a postfix operator, similar to @samp{*} except that it must match 260 is a postfix operator, similar to @samp{*} except that it must match