简体   繁体   English

Reg Exp优化

[英]Reg Exp optimisation

I need to optimise the following reg exp so that it executes faster. 我需要优化以下reg exp,以使其执行更快。 Can anyone help? 有人可以帮忙吗?

([\d\w]{15}[\x01]\d{12}[\x01]\d{2}(.){6}((13((0[0-9]|([1-4][0-9])|5[0-9]))|14((0[0-9]|([1-2][0-9])|30)))[0-5][0-9])801(?:.*))

Thanks 谢谢

This is an optimized version: removed a lot of redundant character classes and groups. 这是一个优化的版本:删除了很多冗余字符类和组。 Ultimately it would be better to know what the regex is supposed to do. 最终,最好知道正则表达式应该做什么。

\w{15}\x01\d{12}\x01\d{2}.{6}(13[0-5]\d|14([0-2]\d|30))[0-5]\d801.*

Edit: based on your new information, you can reduce it further down to: 编辑:根据您的新信息,您可以将其进一步缩减为:

\w{15}\x01\d{12}\x01\d{2}.{6}(13[0-5]\d|1400)[0-5]\d801.*

Without knowing what you want your regex to do it's hard to optimise it, but you could try passing RegexOptions.Compiled to the Regex constructor. 不知道您想让正则表达式做什么,就很难对其进行优化,但是您可以尝试将RegexOptions.Compiled传递给Regex构造函数。 This will take longer to construct the regex object but will mean it performs it's searches more quickly. 这将花费更长的时间来构造regex对象,但是这意味着它将更快地执行其搜索。

The reg exp searches in a text file for records who have the following format and values: reg exp在文本文件中搜索具有以下格式和值的记录:

The record should start with 15 digits or characters, followed by a special character [\\x01] followed by 12 digits and [\\x01] again, followed by 2 digits and any 6 characters, then look for values (date values in the format Hours Minutes Seconds without any : or . separating the hours and minutes and seconds in the file) between 130000 and 140059 which is the part of the reg exp I extracted below, and lastly the value 801 and any number of characters that may follow. 记录应以15位数字或字符开头,后跟特殊字符[\\x01]然后再输入12位数字和[\\x01] ,然后再输入2位数字和任意6个字符,然后查找值(小时值格式的日期值)分钟秒数,不带任何:.分隔文件中的小时,分​​钟和秒)在130000和140059之间,这是下面提取的reg exp I的一部分,最后是值801和随后可能出现的任意数量的字符。

((13((0[0-9]|([1-4][0-9])|5[0-9]))|14((0[0-9]|([1-2][0-9])|30)))[0-5][0-9])

Hope I have been clear and understandable here. 希望我在这里已经清楚易懂。

Also I added a ^ at the start of the reg exp and $ for the end. 另外,我在reg exp的开头添加了^,在结尾添加了$。

I used the RegexOptions.Compiled options as well but not much improvement. 我也使用了RegexOptions.Compiled选项,但没有太大的改进。

Thanks for all the tips, I will try them out. 感谢所有提示,我将尝试它们。 In the meantime if you have any more optimizations to suggest, be most welcome. 同时,如果您还有其他优化建议,欢迎您。

是的,抱歉,我的失误时间部分在130000到143059之间,我将尝试您的解决方案bluepnume。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM