using System;
using System.Text.RegularExpressions;
using System.Threading;
namespace iWare_SCADA_BusinessLogical
{
public static class RegexExtension
{
///
/// 启动一个线程执行操作,同时设置超时时间。
/// var r = WithTimeout(() => regex.Match(foo), 1000);
///
///
///
///
///
private static TR WithTimeout
(Func
proc, int duration)
{
var reset = new AutoResetEvent(false);
var r = default(TR);
Exception ex = null;
var t = new Thread(() =>
{
try
{
r = proc();
}
catch (Exception e)
{
ex = e;
}
reset.Set();
});
t.Start();
if (!reset.WaitOne(duration))
{
t.Abort();
throw new TimeoutException();
}
if (ex != null)
{
throw ex;
}
return r;
}
///
/// 判断正则是否匹配, duration毫秒后报超时异常
///
///
///
///
///
///
public static bool IsMatchWithTimeout(this Regex regex, string input, int startat, int duration)
{
return regex.IsMatchWithTimeout(input, 0, duration);
}
///
/// 判断正则是否匹配, duration毫秒后报超时异常
///
///
///
///
///
public static bool IsMatchWithTimeout(this Regex regex, string input, int duration)
{
if (regex == null)
throw new NullReferenceException();
return WithTimeout(() => regex.IsMatch(input), duration);
}
///
/// 匹配正则, duration毫秒后报超时异常
///
///
///
///
///
///
public static Match MatchWithTimeout(this Regex regex, string input, int startat, int duration)
{
if (regex == null)
throw new NullReferenceException();
return WithTimeout(() => regex.Match(input, startat), duration);
}
///
/// 匹配正则, duration毫秒后报超时异常
///
///
///
///
///
public static Match MatchWithTimeout(this Regex regex, string input, int duration)
{
return regex.MatchWithTimeout(input, 0, duration);
}
///
/// 匹配正则, duration毫秒后报超时异常
///
///
///
///
///
///
public static MatchCollection MatchesWithTimeout(this Regex regex, string input, int startat, int duration)
{
if (regex == null)
throw new NullReferenceException();
return WithTimeout(() => regex.Matches(input, startat), duration);
}
///
/// 匹配正则, duration毫秒后报超时异常
///
///
///
///
///
public static MatchCollection MatchesWithTimeout(this Regex regex, string input, int duration)
{
return regex.MatchesWithTimeout(input, 0, duration);
}
///
///
///
///
///
///
///
///
public static bool IsMatch(string input, string pattern, RegexOptions options, int duration)
{
var regex = new Regex(pattern, options);
return regex.IsMatchWithTimeout(input, duration);
}
///
///
///
///
///
///
///
public static bool IsMatch(string input, string pattern, int duration)
{
return IsMatch(input, pattern, RegexOptions.None, duration);
}
///
///
///
///
///
///
///
///
public static Match Match(string input, string pattern, RegexOptions options, int duration)
{
var regex = new Regex(pattern, options);
return regex.MatchWithTimeout(input, duration);
}
///
///
///
///
///
///
///
public static Match Match(string input, string pattern, int duration)
{
return Match(input, pattern, RegexOptions.None, duration);
}
///
///
///
///
///
///
///
///
public static MatchCollection Matches(string input, string pattern, RegexOptions options, int duration)
{
var regex = new Regex(pattern, options);
return regex.MatchesWithTimeout(input, duration);
}
///
///
///
///
///
///
///
public static MatchCollection Matches(string input, string pattern, int duration)
{
return Matches(input, pattern, RegexOptions.None, duration);
}
}
}