How to check that a regular expression fall into infty loop or not?
I开发者_开发问答'm trying to learn and hopefully use regular expression with Qt.
I found that some patterns cause handler to fall into loop. for example searching \b\b
in a text, never will terminate. and number of these expressions is infinite (\b\b\b
,\b\b\b\b
and so on).
I'd like to determine and control all invalid expressions.
Is there an algorithm to distinguish valid and invalid expressions? If not, how can I prevent my program from falling into an infinite loop?
Regular expressions shouldn't go into an infinite loop, but they can take a long time to evaluate.
I'd suggest using an API with a time out / step limit. If you don't have access to such an API then run the code in a separate thread or process and kill it if it doesn't respond in a timely fashion.
It sounds like there is a bug in the regular expression engine you are using if searching for \b\b
causes an infinite loop.
You can use any of the online Regular Expression Testing Tools like this one. Or download something like this and customize: RegEx Builder (open source)
精彩评论