-1

I need a regex that can match a string of numbers from 1 to 1000 separated by commas.

eg : 12,56,100,190,900,1000

I am using javascript on the front end and php on the back end for validation. Ideally, I need a common regex, which will work for both.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
Saurabh Singh
  • 31
  • 2
  • 9
  • 1
    This isn't a question. What are you trying to do what? What isn't working? – Richard Oct 16 '12 at 07:03
  • Also please add more info: Which language are you using? Why regex and not a CSV parser? – Tim Pietzcker Oct 16 '12 at 07:06
  • i need to allow only number between 1 to 1000 separated by commas in a html field – Saurabh Singh Oct 16 '12 at 07:08
  • Yes, you said that before (except for the HTML bit - what does *that* have to do with it? HTML and CSV are two very different concepts). You didn't answer our questions, though. Please provide more info. – Tim Pietzcker Oct 16 '12 at 07:09
  • i am using regex because i need it to allow values from 1 to 1000 separated by commas in a html field – Saurabh Singh Oct 16 '12 at 07:09
  • i cant use a csv parser to validate input in html elements. right? – Saurabh Singh Oct 16 '12 at 07:10
  • Oh dear, this isn't going anywhere useful. Please: Show an excerpt of the *actual* data you have. Show the code you're using so far to deal with it. Show what isn't working the way you expect it to. Otherwise, there's not much we can do. – Tim Pietzcker Oct 16 '12 at 07:10
  • Not a lot of info in this question, but... [This site](http://www.regular-expressions.info/numericranges.html) explains how to match multi-digit numbers with regular expressions. [This StackOverflow question](http://stackoverflow.com/questions/676467/how-to-match-numbers-between-x-and-y-with-regexp) asks how to match multi-digit numbers with regular expressions. [This site](http://utilitymill.com/utility/Regex_For_Range) generates the regular expressions for checking numbers in a range. And have you considered that regular expressions may not be the best way of doing this? Split the string on – Richard Oct 16 '12 at 07:15

2 Answers2

3

Numbers in the range 1-1000, separated commas, is matched by

(?<=,|^)([1-9][0-9]{0,2}|1000)(?=,|$)
carlpett
  • 12,203
  • 5
  • 48
  • 82
  • 1
    good, unless OP wants it in javascript – Michal Klouda Oct 16 '12 at 07:16
  • 1
    And it doesn't validate whether the entire string contains only valid numbers. It will just pick the valid numbers from it. If it's applied repeatedly to the string. The original question is so vague that it's hard to tell whether this matters, but the word "validation" gives me pause. One single correct value would be enough for the regex to report success, quite possibly not what Saurabh wants. – Tim Pietzcker Oct 16 '12 at 07:19
  • This pattern will match `123,abc` - but he needs to have a CSV line of numbers. – Aleks G Oct 16 '12 at 07:20
  • Thanks carlpett and Tim. FYI i am using javascript on the front end and php on the backend to validate this input and i need a common regex which can work for both is it possible. – Saurabh Singh Oct 16 '12 at 07:21
  • @SaurabhSingh: This regex requires lookahead and lookbehind, but since we do not know exactly what strings are accepted and which (if any) should not match at all, it was just a guess as to what you could want. So, what is ok, and what is not? What about spaces? – carlpett Oct 16 '12 at 07:31
  • @SaurabhSingh: Why didn't you write this in your question? That is the place where this information is needed. Otherwise *we cannot help you* without becoming extremely frustrated. – Tim Pietzcker Oct 16 '12 at 07:32
  • @TimPietzcker: sorry Tim i'll put it up there as well – Saurabh Singh Oct 16 '12 at 07:34
2

If you want to match the entire line, than something like this should do:

^([1-9][0-9]{0,2}|1000)(,([1-9][0-9]{0,2}|1000))*$

Depending on your requirements, you may also want to allow whitespace in the beginning, end, and/or after commas.

This will allow whitespace in the beginning:
^\s*([1-9][0-9]{0,2}|1000)(,([1-9][0-9]{0,2}|1000))*$

This will allow whitespace at the end:
^([1-9][0-9]{0,2}|1000)(,([1-9][0-9]{0,2}|1000))*\s*$

This will allow whitespaces after commas:
^([1-9][0-9]{0,2}|1000)(,\s*([1-9][0-9]{0,2}|1000))*$

Combine these to your liking.

EDIT 2: If you want to allow a comma in the binning or at the end, then your regex becomes

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

Here, ,? means that you can have 0 or 1 comma.

EDIT: explanation, as requested:

  • ^ in the beginning and $ at the end are start/end of input marks - they ensure that we test the entire input
  • Parentheses work just as you would expect them
  • [1-9] matches a digit 1 through 9, similarly [0-9] matches a digit 0 through 9
  • {0,2} indicates that the previous part (in our case [0-9]) is present between 0 and 2 times
  • | is a logical OR - either part before it matches or the part after it

Thus in the first set of parentheses we match digit 1 to 9 followed by 0, 1 or 2 digits 0 to 9 - this gives us numbers between 1 and 999 - or we match 1000.

Then we match a comma followed by the same block as described above - and this lot is matched 0 or more times - as indicated by * character after the parentheses.

Aleks G
  • 56,435
  • 29
  • 168
  • 265