Timeout regular expression within 4

Possible duplicate:
Deploying RegEx Timeout in .NET 4

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

"System.Text.RegulerExpression.Regex" does not contain a constructor that takes 3 arguments. Note. The error is in structure 4. If you use the 4.5 framework, you will not encounter this error. But I used framework 4 and I need to set the regexpr timeout. What is the remedy for this?

+2
source share
1 answer

There is no constructor like the one you use in .NET 4. Look at the documentation page ; the only options for the constructor are:

Regex ()

Regex (String)

Regex (SerializationInfo, StreamingContext)

Regex (String, RegexOptions)

EDIT

You can use Task to run the regex method and Wait to pass a timeout. Something like this should do the job:

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

Source: https://habr.com/ru/post/909427/


All Articles