它掛,因爲這(\w+)+
場景。
I.e. 太複雜失敗。
工作正常,如果匹配,失敗時炸燬。
This (\w,?\s?)+
與(\w+,?\s?)+
完全相同,但不會掛起。
因此,將其更改爲此^(\w,?\s?)+(?=:): hi hey\?$
並解決問題。
作爲獎勵,這^(\w,?\s?)+: hi hey\?$
是相同的。
此外,您可以用.*?\?$
代替您的文字hi hey\?$
如果預期爲變量字面值。
Error: Target Operation ..
The complexity of matching the regular
expression exceeded predefined bounds.
Try refactoring the regular expression
to make each choice made by the state
machine unambiguous. This exception is
thrown to prevent "eternal" matches that
take an indefinite period time to
locate.
編輯:
注意,有總是與嵌套量詞一個潛在的問題。
I.e.那些貪婪和開放式的,如(b +
)*
。
這幾乎可以通過刪除一個內部的巢(如在示例b+
)解決。
通過使它不被量化,我們可以稱之爲僞錨。
也就是說,它應該是該組中的第一個並且是一個未定量的必需字符。
這會迫使發動機回原點那字符再次檢查它。
如果沒有量化,立即放棄,甚至不會看
表達式的其餘部分。
因此,它經過字符串中的該位置以找到下一個字面值b
。
基本上這是什麼回溯治療是全部。
鑑於回溯陷阱,我們可以做出解決方案來獲得所需的匹配。
^\s*(\w+\s*(?:[,\s]\s*\w+\s*)+)\s*:\s*([^:]*?\w[^:]*?)\s*\?\s*$
Formatted
^ # BOS
\s* # Wsp trim
( # (1 start), Values - minimum of 2 required
\w+ \s* # First word
(?: [,\s] \s* \w+ \s*)+ # One or more space or comma seperated
# word value's
) # (1 end)
\s* # Wsp trim
: # Colon
\s* # Wsp trim
( # (2 start), Question -
[^:]*? # Not a colon
\w # At least a word char
[^:]*? # Not a colon
) # (2 end)
\s* # Wsp trim
\? # '?'
\s* # Wsp trim
$ # EOS
來源
2017-05-18 16:00:49
sln
雖然'(?= :)'先行是多餘這裏,主要的問題是,'(\ w +,?\ s?)+'包含一個強制性的' \ w +',其餘是可選的。這是導致災難性回溯的典型模式。使用['^ \ w +(?:,\ s * \ w +)*:。* \?$'](https://regex101.com/r/EhtNUw/2)。 –
我不是很流利的正則表達式,但[this](http://stackoverflow.com/questions/8316284/why-regex-ismatch-hangs)可能與 – Wondercricket
有關係。您可以使用:['^ \ w +(? :\ s *,\ s * \ w +)* \ s *:\ s *([^?] + \?)'](https://regex101.com/r/JqnwnT/2) – anubhava