I need to check a string that must match:
1,2,3,4,5,6,7
So the rules are: only one number between 1 and 7, a comma separator, but I don't want repetition, so it's not legal to write:
1,2,3,1,5
Currently I'm using:
([1-7]{1},){1,6}([1-7]{1})
How can I modify it in order to check for repetition?
I think @tobias_k is on the right track by suggesting that it's a good strategy to check for strings that have repetition, but I think you can have a more comprehensive regular expression that limits the character class and delimiter as you require.
Update: This version checks for occurrence of multiple digits.
([1-7]{1})(?:[1-7,])*\1|[^1-7,]+
([1-7]{1}),(?:[1-7]{1},)*(?:\1|[^1-7,]|(?:[1-7]{2,}))
1,2,3,4,5,6,7 // PASS: no match
1,2,3,4,5,6,8 // FAIL: matches out-of-bounds digit '8'
1,2,3,four,5,6,7 // FAIL: matches non-integer 'four'
1,2,3,1,5 // FAIL: matches repeating digit '1'
1,2,3,45 // FAIL: matches multiple digits
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments