2012-11-23 38 views
2

可能重複:
Implementing RegEx Timeout in .NET 4超時正則表達式4

Regex regexpr = new Regex(anchorPattern[item.Key], RegexOptions.Singleline, TimeSpan.FromMilliseconds(10)); 

「System.Text.RegulerExpression.Regex」 不包含建築工認爲接受3個參數。 注意:錯誤發生在框架4中。如果使用框架4.5,則不會遇到此錯誤。 但我一直在使用框架4,我必須設置超時regexpr。這是什麼補救措施?

+1

[使用.NET實施的正則表達式超時4]看看(http://stackoverflow.com/questions/9460661/implementing-regex-timeout-in-net- 4) - 這有一個如何實現這個自己的例子。 (注意,它不會讓你創建一個''正則表達式'',但它可以讓你超時調用'Match'等)。 – Rawling

回答

3

在.NET 4中沒有這樣的構造函數。請看documentation page;爲構造函數中的唯一選項是:

正則表達式()

正則表達式(字符串)

正則表達式(SerializationInfo中,的StreamingContext)

正則表達式(字符串,RegexOptions)

編輯

您可以使用Task運行正則表達式和Wait方法來傳遞超時。這樣的事情應該做的工作:

var regexpr = new Regex(anchorPattern[item.Key], RegexOptions.Singleline); 
var task = Task.Factory.StartNew(()=>regexpr.Match(foo)); 
var completedWithinAllotedTime = task.Wait(TimeSpan.FromMilliseconds(10)); 
+0

我知道fw 4.0中的regex構造器。我一直在問如何在框架4中設置超時值爲正則表達式。 – RockOnGom

+0

@alikoyuncu,請參閱最新的編輯。 – RePierre

+0

謝謝@RePierre。我解決了麻煩。 – RockOnGom