普通的前pression其中字符串的一部分必须是0-100之间的数 [英] Regular expression where part of string must be number between 0-100

查看:132
本文介绍了普通的前pression其中字符串的一部分必须是0-100之间的数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证序列号。为此,我们使用常规的前pressions在C#中,并有一定的产品,序列号的部分是午夜以来秒。有86400秒的一天,但我怎么能验证它在这个字符串中的5位数字:

I need to validate serial numbers. For this we use regular expressions in C#, and a certain product, part of the serial number is the "seconds since midnight". There are 86400 seconds in a day, but how can I validate it as a 5-digit number in this string?:

654984051-86400-231324

我不能使用这一概念:

I can't use this concept:

[0-8][0-6][0-4][0-0][0-0]

由于然后 86399 将是无效的。我该如何克服这个问题?我想是这样的:

Because then 86399 wouldn't be valid. How can I overcome this? I want something like:

[00000-86400]

更新结果
我想要说清楚,我所知道的 - 并且同意 - 的的不使用常规的前pressions时,有一个简单的方法的学校的深思熟虑。 <一href=\"http://stackoverflow.com/questions/1909528/regular-ex$p$pssion-where-part-of-string-must-be-number-between-0-100#1909559\">Jason's回答是我究竟是如何想做到这一点,但此序列号验证是对流经我们的系统所有序列号 - 目前尚无自定义验证code为这些特定的人。的在这种情况下的我有一个很好的理由,寻找一个正则表达式的解决方案。

UPDATE
I want to make it clear that I'm aware of - and agree with - the "don't use regular expressions when there's a simpler way" school-of-thought. Jason's answer is exactly how I'd like to do it, however this serial number validation is for all serial numbers that pass through our system - there's currently no custom validation code for these specific ones. In this case I have a good reason for looking for a regex solution.

当然,如果没有一个,那么这使得对这些不可否认的特定产品定制验证的情况,但我想完全需要code变化的解决方案之前,去探索这一途径。

Of course, if there isn't one, then that makes the case for custom validation for these particular products undeniable, but I wanted to explore this avenue fully before going with a solution that requires code changes.

推荐答案

生成一个普通防爆pression以匹配任意的数字范围
<一href=\"http://utilitymill.com/utility/Regex%5FFor%5FRange\">http://utilitymill.com/utility/Regex_For_Range

产生以下的正则表达式前pression:

yields the following regex expression:

\b0*([0-9]{1,4}|[1-7][0-9]{4}|8[0-5][0-9]{3}|86[0-3][0-9]{2}|86400)\b

输出的说明:

First, break into equal length ranges:
  0 - 9
  10 - 99
  100 - 999
  1000 - 9999
  10000 - 86400

Second, break into ranges that yield simple regexes:
  0 - 9
  10 - 99
  100 - 999
  1000 - 9999
  10000 - 79999
  80000 - 85999
  86000 - 86399
  86400 - 86400

Turn each range into a regex:
  [0-9]
  [1-9][0-9]
  [1-9][0-9]{2}
  [1-9][0-9]{3}
  [1-7][0-9]{4}
  8[0-5][0-9]{3}
  86[0-3][0-9]{2}
  86400

Collapse adjacent powers of 10:
  [0-9]{1,4}
  [1-7][0-9]{4}
  8[0-5][0-9]{3}
  86[0-3][0-9]{2}
  86400

Combining the regexes above yields:
  0*([0-9]{1,4}|[1-7][0-9]{4}|8[0-5][0-9]{3}|86[0-3][0-9]{2}|86400)

在这里测试:
http://osteele.com/tool​​s/rework/

这篇关于普通的前pression其中字符串的一部分必须是0-100之间的数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆