Solution for regular expression matching without exception resource depletion

Source: Internet
Author: User
In c #, regular expressions are used for matching. Sometimes we encounter this situation. The cpu usage is 100%, but the regular expression does not throw an exception and the regular expression remains in the matching process, this will cause system resources to be exhausted and applications to get stuck. This is because the regular expressions do not fully match and the Regex does not have the Timeout attribute, causing the regular expression processor to fall into an endless loop.
This situation may especially occur in the process of matching unreliable matched objects. For example, in my personal website www.eahan.com, automatic acquisition and matching of multiple website pages, this problem often occurs. To avoid resource depletion, I wrote an AsynchronousRegex class, as the name suggests, asynchronous Regex. Set a Timeout attribute for this class and place the Regex matching action in a separate thread. AsynchronousRegex monitors the destruction of threads when the Regex matching exceeds the Timeout limit.
Using System;
Using System. Text. RegularExpressions;
Using System. Threading;
Namespace LZT. Eahan. Common
{
Public class AsynchronousRegex
{
Private MatchCollection mc;
Private int _ timeout; // The longest sleep time (timeout), in milliseconds
Private int sleepCounter;
Private int sleepInterval; // sleep interval, in milliseconds
Private bool _ isTimeout;
Public bool IsTimeout
{
Get {return this. _ isTimeout ;}
}
Public AsynchronousRegex (int timeout)
{
This. _ timeout = timeout;
This. sleepCounter = 0;
This. sleepInterval = 100;
This. _ isTimeout = false;
This. mc = null;
}
Public MatchCollection Matchs (Regex regex, string input)
{
Reg r = new Reg (regex, input );
R. OnMatchComplete + = new Reg. MatchCompleteHandler (this. MatchCompleteHandler );

Thread t = new Thread (new ThreadStart (r. Matchs ));
T. Start ();
This. Sleep (t );
T = null;
Return mc;
}
Private void Sleep (Thread t)
{
If (t! = Null & t. IsAlive)
{
Thread. Sleep (TimeSpan. FromMilliseconds (this. sleepInterval ));
This. sleepCounter ++;
If (this. sleepCounter * this. sleepInterval> = this. _ timeout)
{
T. Abort ();
This. _ isTimeout = true;
}
Else
{
This. Sleep (t );
}
}
}
Private void MatchCompleteHandler (MatchCollection mc)
{
This. mc = mc;
}
Class Reg
{
Internal delegate void MatchCompleteHandler (MatchCollection mc );
Internal event MatchCompleteHandler OnMatchComplete;
Public Reg (Regex regex, string input)
{
This. _ regex = regex;
This. _ input = input;
}
Private string _ input;
Public string Input
{
Get {return this. _ input ;}
Set {this. _ input = value ;}
}
Private Regex _ regex;
Public Regex
{
Get {return this. _ regex ;}
Set {this. _ regex = value ;}
}
Internal void Matchs ()
{
MatchCollection mc = this. _ regex. Matches (this. _ input );
If (mc! = Null & mc. Count> 0) // The cpu resource may be exhausted.
{
This. OnMatchComplete (mc );
}
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.