Reputation: 57
Could anybody help me with regular expression as mentioned.
I have an xml element 'docs' which has an attribute 'range' where range should take only values in the below format.
range = "1,2,3,4,5"
or
range = "1,2,3-6,10-12,15,20-30"
or
range = "all"
A littel help will be much appreciated. Thanks in advance.
Upvotes: 1
Views: 210
Reputation: 5768
^\d(?!.*,-)[\d\,\-]*\d$|^all$|^\d+$
Should work for your numbers. Input can only be numbers, commas, or dash or the word all
.
Upvotes: 3
Reputation: 54790
As @minopret mentioned, this is probably best not handled completely with regex. I would split(",")
the strings and then iterate over the resulting array to make sure that they are in ascending order. You can regex each of the individual array elements at that point to make sure they match something like:
\d+(-\d+)?
Upvotes: 1
Reputation: 4806
all|[1-9][0-9]*(-[1-9][0-9]*)?(,[1-9][0-9]*(-[1-9][0-9]*)?)*
I have assumed that zero and leading zeroes are not permitted.
Regular expressions are not a good way to ensure that numbers are in ascending order.
Upvotes: 2