Friday, December 29, 2017

java How to validate dash separated values using regex

java How to validate dash separated values using regex




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]+)*$

heres solution:

^(?:d+(?:-d+)?(?:,|$))+$

demo

explanation: match number, optionally followed dash , number, match either comma or end of string. , repeat.

java regex

go to link download
download
alternative link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.