正则表达式进行大文件查找时的超时处理
2010-06-07 09:51
218 查看
应用系统服务器端的日志查找有很多的实现方式,限于软件、资源的问题,决定自己开发个远程日志单点查看工具,实现原理:
服务端职责:提供消息接收、监听,提供日志搜索查找与反馈
客户端职责:发送查询指令,接收查询结果
UI:提供关键字(支持正则表达式)、全字、大小写查找,可设定开始时间、可搜索的机器及其目录……
其中C/S通过Socket的方式连接通讯。
因为提供到正则表达式支持,所以在不正确的输入正则表达式时,会造成搜索查找非常耗时,所以在附加正则时,需要进行超时处理,具体如何实现,且看下文:
服务端职责:提供消息接收、监听,提供日志搜索查找与反馈
客户端职责:发送查询指令,接收查询结果
UI:提供关键字(支持正则表达式)、全字、大小写查找,可设定开始时间、可搜索的机器及其目录……
其中C/S通过Socket的方式连接通讯。
因为提供到正则表达式支持,所以在不正确的输入正则表达式时,会造成搜索查找非常耗时,所以在附加正则时,需要进行超时处理,具体如何实现,且看下文:
public class AsynchronousRegex { private MatchCollection mc; private bool isMatch = false; private int _timeout; private int sleepCounter = 0; private int sleepInterval = 100; public bool IsTimeout { get; set; } public AsynchronousRegex(int timeout) { this._timeout = timeout; this.IsTimeout = false; this.mc = null; } /// <summary> /// get matches items /// </summary> /// <param name="input"></param> /// <param name="regex"></param> /// <param name="options"></param> /// <returns></returns> public MatchCollection Matches(string input, string regex, RegexOptions options) { Reg r = new Reg(regex, input, options); r.OnMatchComplete += new Reg.MatchCompleteHandler(this.MatchCompleteHandler); Thread t = new Thread(new ThreadStart(r.Matchs)); t.Start(); this.Sleep(t); t = null; return mc; } /// <summary> /// check match /// </summary> /// <param name="input"></param> /// <param name="regex"></param> /// <param name="options"></param> /// <returns></returns> public bool IsMatch(string input, string regex, RegexOptions options) { Reg r = new Reg(regex, input, options); r.OnIsMatchComplete += new Reg.IsMatchCompleteHandler(this.IsMatchCompleteHandler); Thread t = new Thread(new ThreadStart(r.IsMatch)); t.Start(); this.Sleep(t); t = null; return isMatch; } 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; } private void IsMatchCompleteHandler(bool _IsMatch) { this.isMatch = _IsMatch; } class Reg { internal delegate void MatchCompleteHandler(MatchCollection mc); internal delegate void IsMatchCompleteHandler(bool isMatch); internal event MatchCompleteHandler OnMatchComplete; internal event IsMatchCompleteHandler OnIsMatchComplete; public Reg(string regex, string input, RegexOptions options) { this.Regexs = regex; this.Inputs = input; this.Options = options; } /// <summary> /// 内容 /// </summary> public string Inputs { get; set; } /// <summary> /// 规则 /// </summary> public string Regexs { get; set; } public RegexOptions Options { get; set; } internal void Matchs() { MatchCollection mc = Regex.Matches(this.Inputs, Regexs, Options); if (mc != null && mc.Count > 0) // 这里有可能造成cpu资源耗尽 { this.OnMatchComplete(mc); } } internal void IsMatch() { Regex regex = new Regex(Regexs, Options); if (regex.IsMatch(this.Inputs)) { this.OnIsMatchComplete(true); } } } }
相关文章推荐
- 利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)
- c# 使用正则表达式对字符串进行处理
- 使用Android Studio查找文件中含有中文字符串位置(正则表达式)
- 文件处理工具、grep、正则表达式:
- 用正则表达式进行批量查找替换
- Day4:正则表达式与文件格式化处理
- 4.正则表达式和文件格式化处理
- 用.net的正则表达式对文章进行拆句处理
- 用正则表达式处理日志文件的小程序
- 正则表达式--小脚本(对输入内容进行处理)
- Excel 中使用正则表达式进行查找与替换
- 第12章 正则表达式与文件格式化处理
- 第12章 正则表达式与文件格式化处理
- LINUX 正则表达式与文件格式化处理(sed / awk / diff )
- Linux中正则表达式与文件格式化处理命令(awk/grep/sed)
- Word中使用正则表达式进行查找和替换与难题征解
- linux备忘录-正则表达式与文件格式化处理
- 在 Notepad++ 或 EditPlus 中使用正则表达式进行高级查找替换
- 文件内容查找(一)grep正则表达式
- 在 Notepad++ 或 EditPlus 中使用正则表达式进行高级查找替换