Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEX To accept numbers separated by commas, but number range is 0-32767

I need to write a regular expression for taking input like this

23,456,22,1,32767

i.e.

  1. No commas allowed at the start or end.
  2. Spaces may come before and/or start of comma for e.g. 23, 45,56 ,67 etc.
  3. Ranges of each number should be 0-32767.

Currently I am using regular expression like this [0-9]+(,[0-9]+)*.

This allows for numbers separated by commas only ( not allowing spaces at all), and it does not check for the range of number.

like image 496
Basmah Avatar asked Jun 07 '11 07:06

Basmah


1 Answers

It's probably wise to do it in two steps. First check that the range is 0-99999:

^[0-9]{1,5}( *, *[0-9]{1,5})*$

Then parse the string to a list of integers using a general purpose programming language and check that x <= 32767 for each integer x.

like image 168
Mark Byers Avatar answered Jan 04 '23 06:01

Mark Byers