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?
Asked
Active
Viewed 654 times
1
-
1Isn't your worst case scenario going to be someone who puts in a complex regex that will have NO matches? Seems like you're better off using some other mechanism to prevent regex searches from consuming runtime. Alternatively, you should find out if regex is actually going to be a significant potential bottleneck before you pre-optimize – Jherico May 26 '09 at 18:26
2 Answers
1
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.
0
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
- 64,182
- 22
- 135
- 226