I need a regex to validate VLAN string entered by user. The string should allow numbers or ranges, separated by comma. The numbers must be between 1 and 4093.
Below samples are allowed:
1,
1,2,3,4
1-10,
1-4093
4000
I tried below :
^0*([1-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-3][0-9]{3}|40[0-8][0-9]|409[0-3])$
Need to enhance for comma separated and ranges
To match a number from 1 to 4093 one can use:
(?:[1-9]\d{0,2}|[1-3]\d{3}|40(?:[0-8]\d|9[0-3]))
That we'll call N
. Now the repetition part:
^(N)(?:[,-] *(N)?)*$
which gives:
^(?:[1-9]\d{0,2}|[1-3]\d{3}|40(?:[0-8]\d|9[0-3]))(?:[,-] *(?:[1-9]\d{0,2}|[1-3]\d{3}|40(?:[0-8]\d|9[0-3]))?)*$