views:

266

answers:

2

Is there anyway to get a regex pattern to automatically stop searching after one match. I'd like to add regex searching to a web service I'm trying to create, but I don't want someone to be able to run a regex that would take a long time, just one match is good enough. Is this possible?

A: 

The really bad degenerate pattern never match. And if you find a good way of finding the degenerate cases, well you will probably be due a lot of money. You are probably better off with a timeout. In Perl I would use alarm combined with a block eval.

You may also be looking for (*COMMIT) in Perl which prevents backtracking.

Chas. Owens
+1  A: 

You could still end up with a long running regex, even using a limiting quantifier.

Implementing an interruptable capability like this thread discusses is another option.

patjbs