如何在1次匹配后停止正則表達式匹配而不使用非貪婪的字符

[英]How to stop regex matching after 1 match without using non-greedy character


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?

無論如何,有一個正則表達式模式在一場比賽后自動停止搜索。我想將正則表達式搜索添加到我正在嘗試創建的Web服務中,但我不希望有人能夠運行需要很長時間的正則表達式,只需一個匹配即可。這可能嗎?

2 个解决方案

#1


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.

真正糟糕的退化模式永遠不會匹配。如果你找到一個很好的方法來找到墮落的案例,那么你可能會花很多錢。超時可能會更好。在Perl中,我會使用alarm和block eval結合使用。

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

你也可能在Perl中尋找(* COMMIT)來阻止回溯。

#2


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.

實現像這個線程討論的可中斷功能是另一種選擇。

关注微信公众号

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/26/7c672a31b291c0595672c19f97d1063d.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com