i need regex match lists of numbers , 1 match ranges of numbers (expressions shall never fail in both cases). ranges shall consist of number, dash, , number (n-n), while lists shall consist of numbers separated comma (n,n,n). here below examples.
ranges:
'1-10' => ok whateverelse => nok (e.g. '1-10 11-20') list:
'1,2,3' => ok whateverelse => nok and here 2 regular expressions:
- [0-9]+[\-][0-9]+
- ([0-9]+,?)+
... have few problems them... example:
when evaluating '1-10', regex 2 matches 1... shouldn't match because string not contain list.
then, when evaluating '1-10 11-14', regex 1 matches 1-10... shouldn't match because string contains more range.
what missing? thanks.
try this:
^((\d+-(\*|\d+))|((\*|\d+)-\d+)|((\d)(,\d)+))$ test results:
1-10 ok 1,2,3 ok 1-* ok *-10 ok 1,2,3 1-10 nok 1,2,3 2,3,4 nok *-* nok visualization of regex:

edit: added wildcard * per op's comment.
Comments
Post a Comment