Regex regexpr = new Regex(anchorPattern[item.Key], RegexOptions.Singleline, TimeSpan.FromMilliseconds(10));
「System.Text.RegulerExpression.Regex」 不包含建築工認爲接受3個參數。 注意:錯誤發生在框架4中。如果使用框架4.5,則不會遇到此錯誤。 但我一直在使用框架4,我必須設置超時regexpr。這是什麼補救措施?
Regex regexpr = new Regex(anchorPattern[item.Key], RegexOptions.Singleline, TimeSpan.FromMilliseconds(10));
「System.Text.RegulerExpression.Regex」 不包含建築工認爲接受3個參數。 注意:錯誤發生在框架4中。如果使用框架4.5,則不會遇到此錯誤。 但我一直在使用框架4,我必須設置超時regexpr。這是什麼補救措施?
在.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));
[使用.NET實施的正則表達式超時4]看看(http://stackoverflow.com/questions/9460661/implementing-regex-timeout-in-net- 4) - 這有一個如何實現這個自己的例子。 (注意,它不會讓你創建一個''正則表達式'',但它可以讓你超時調用'Match'等)。 – Rawling