Reputation: 5974
I want to accept 0-9
,
and -
So I have:
[0-9-,]+
However I only want there to be ever one -
in a row, so you can't have --
. How can I do this?
Upvotes: 1
Views: 129
Reputation: 5236
I think required one is this:
^\d+(-\d+)?(,\d+(-\d+)?)*$
What this does is:
\d+
any integer (one or more digits)
(-\d+)?
optional part matching a -
followed by an integer
(,\d+(-\d+)?)*
zero or more occurrence of a ,
followed by combination of above mentioned patterns.
Note: Add required escaping for \
Upvotes: 2
Reputation: 195029
is this ok ?
^[0-9,]*-?[0-9,]*$
just did a small test with grep:
kent$ echo "1-234-
1234-
3-24442-34
12341234"|grep -E '^[0-9,]*-?[0-9,]*$'
1234-
12341234
Upvotes: 0
Reputation: 336098
Use a lookahead assertion:
(?!.*--)[0-9,-]+
Also, you might need to use anchors to ensure that the entire string is matched:
^(?!.*--)[0-9,-]+$
Upvotes: 2