开发者

How to write regex to match number ranges?

I am using the asp.net RegularExpressionValidator to check for input on a multiline textbox. I ALMOST have it, but I must be missing something. This is how it SHOULD be:

1 => valid

1-10 => valid

1-10,45,50 => valid

1 10 45 50 => valid

111 => INVALID

However, 111 is coming back Valid

Here is the regex I am using: "(([0-9]{1,2})(,|\s|-)?)*" and it works for everything but 111, where it is saying valid. I know 开发者_开发百科why it thinks it's valid, I just don't know how to make that invalid.


Don't make the seperator optional, then you will match number-nothing-number, and therefore match 111, instead do something like:

"[0-9]{1,2}([-,\s][0-9]{1,2})*"

edit: for clarity: If your regex engine does not match the whole string, you need to add ^ at the start, and $ at the end of the regex.


Your regex as written is matching "11" from "111" and zero occurrences of the repeating pattern. If you wrap your regex in ^...$ it should work:

^(([0-9]{1,2})(,[\s-])?)*$

Since then the regex will not permit any additional characters in the string.


Left-anchor your pattern with ^. The * is greedy and you're matching on the second and third 1 in 111.


Maybe to make things a bit simpler, break it up into multiple regex checks?

If string matches (([0-9]{1,2})(,|\s|-)?)*, then check if string matches \d{3,}. If it does, then fail else pass.


I think you need to require the delimiter. '?' allows zero or one instances.


I think there is two issues. One issue is a regex that validates on each character a user enters. Another regex to extract the data afterwards.

To use a regex that validates upon each enterred character, the end anchor is more important than the beginning, although the beginning anchor, unwanted characters can get in.

So on rubular.com, this particular regex allows for [, -] characters using the rules

  1. no more than 2 digits in a row
  2. commma or space or dash, not more than 2 in a row

In the end, data extraction will be different, so this just works on a user entered per-character basis.

It worked on everything I tried as far as variants.

^(?:\b\d{1,2}(?:([, -])(?!\1)[, -]?|))+$

The \b is indeed necessary to distinquish 3 digits in a row.
Try it out.


After your [0-9]{1,2} term, you need a negative lookahead to ensure that the next character isn't a digit as well. I'm not familiar with asp.net, but something like [0-9]{1,2}(?![0-9]) might yield better results. See this page for more information about lookahead.

Edit: The regex posted above helps, but still matches the last two digits of 111. The following regex should work (as verified using an online regex tester). It adds a word-boundary anchor at the beginning, which should prevent the expression from starting a match in the middle of a number. It also makes the delimiter mandatory instead of optional. This combination removes the need for the lookahead.

(\b([0-9]{1,2})(-|,|\s|$))+
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜