java - How to validate dash-separated values using regex? -
i have string digits , separators. digits may either separated comma
or hyphen
. there may never 2 digits both separated hyphens without comma in between.
example:
valid: 123,12,2,1-3,1,1-3,1
invalid: 123,12,2,1-3,1,1-3-5,1
i have regex works, except not observe 1-3-5
invalid lines.
how can improve following?
^([0-9])+((,|-)[0-9]+)*$
here's solution:
^(?:\d+(?:-\d+)?(?:,|$))+$
demo
explanation: match number, optionally followed dash , number, match either comma or end of string. , repeat.
java regex
No comments:
Post a Comment