using System;
|
using System.Text.RegularExpressions;
|
using System.Threading;
|
|
namespace iWare_SCADA_BusinessLogical
|
{
|
public static class RegexExtension
|
{
|
/// <summary>
|
/// 启动一个线程执行操作,同时设置超时时间。
|
/// var r = WithTimeout(() => regex.Match(foo), 1000);
|
/// </summary>
|
/// <typeparam name="TR"></typeparam>
|
/// <param name="proc"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
private static TR WithTimeout<TR>(Func<TR> 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;
|
}
|
|
/// <summary>
|
/// 判断正则是否匹配, duration毫秒后报超时异常
|
/// </summary>
|
/// <param name="regex"></param>
|
/// <param name="input"></param>
|
/// <param name="startat"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static bool IsMatchWithTimeout(this Regex regex, string input, int startat, int duration)
|
{
|
return regex.IsMatchWithTimeout(input, 0, duration);
|
}
|
|
/// <summary>
|
/// 判断正则是否匹配, duration毫秒后报超时异常
|
/// </summary>
|
/// <param name="regex"></param>
|
/// <param name="input"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static bool IsMatchWithTimeout(this Regex regex, string input, int duration)
|
{
|
if (regex == null)
|
throw new NullReferenceException();
|
|
return WithTimeout(() => regex.IsMatch(input), duration);
|
}
|
/// <summary>
|
/// 匹配正则, duration毫秒后报超时异常
|
/// </summary>
|
/// <param name="regex"></param>
|
/// <param name="input"></param>
|
/// <param name="startat"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
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);
|
}
|
|
/// <summary>
|
/// 匹配正则, duration毫秒后报超时异常
|
/// </summary>
|
/// <param name="regex"></param>
|
/// <param name="input"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static Match MatchWithTimeout(this Regex regex, string input, int duration)
|
{
|
return regex.MatchWithTimeout(input, 0, duration);
|
}
|
|
/// <summary>
|
/// 匹配正则, duration毫秒后报超时异常
|
/// </summary>
|
/// <param name="regex"></param>
|
/// <param name="input"></param>
|
/// <param name="startat"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
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);
|
}
|
|
/// <summary>
|
/// 匹配正则, duration毫秒后报超时异常
|
/// </summary>
|
/// <param name="regex"></param>
|
/// <param name="input"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static MatchCollection MatchesWithTimeout(this Regex regex, string input, int duration)
|
{
|
return regex.MatchesWithTimeout(input, 0, duration);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="input"></param>
|
/// <param name="pattern"></param>
|
/// <param name="options"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static bool IsMatch(string input, string pattern, RegexOptions options, int duration)
|
{
|
var regex = new Regex(pattern, options);
|
return regex.IsMatchWithTimeout(input, duration);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="input"></param>
|
/// <param name="pattern"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static bool IsMatch(string input, string pattern, int duration)
|
{
|
return IsMatch(input, pattern, RegexOptions.None, duration);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="input"></param>
|
/// <param name="pattern"></param>
|
/// <param name="options"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static Match Match(string input, string pattern, RegexOptions options, int duration)
|
{
|
var regex = new Regex(pattern, options);
|
return regex.MatchWithTimeout(input, duration);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="input"></param>
|
/// <param name="pattern"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static Match Match(string input, string pattern, int duration)
|
{
|
return Match(input, pattern, RegexOptions.None, duration);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="input"></param>
|
/// <param name="pattern"></param>
|
/// <param name="options"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static MatchCollection Matches(string input, string pattern, RegexOptions options, int duration)
|
{
|
var regex = new Regex(pattern, options);
|
return regex.MatchesWithTimeout(input, duration);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="input"></param>
|
/// <param name="pattern"></param>
|
/// <param name="duration"></param>
|
/// <returns></returns>
|
public static MatchCollection Matches(string input, string pattern, int duration)
|
{
|
return Matches(input, pattern, RegexOptions.None, duration);
|
}
|
}
|
}
|