开发者

Reg Exp optimisation

I need to optimise the following reg exp so that it executes faster. Can anyone help?

([\d\开发者_StackOverflow中文版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. This will take longer to construct the regex object but will mean it performs it's searches more quickly.


The reg exp searches in a text file for records who have the following format and values:

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.

((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.

I used the RegexOptions.Compiled options as well but not much improvement.

Thanks for all the tips, I will try them out. In the meantime if you have any more optimizations to suggest, be most welcome.


Yes sorry, a mistake on my part The time part is between 130000 and 143059 I will try out your solution, bluepnume.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜