I assume the empty string is valid. I'm not sure I understand your third article; do you mean that n can be anything, or do you need to limit things to n occurrences? I'm also not sure how many digits there should be at each end of the hyphen; is it any number one or more, or exactly one?
The following regular expression allows, for example, to use a string, for example, 1-9-129-2-293-23.
^(([0-9]+-[0-9]+)|[0-9]+)*$
Since each subpattern must begin and end with a number, there is no need to have a bit digit at each end outside the substring, as in other solutions posted here.
source share