Reputation: 38094
I wrote Regex to allow only digits. It is @"[0-9]"
and it works perfectly. But I would like to add a symbol "-" in regex. I want to allow digits and "-". How to do it?)
I've tried such mask @"[0-9]\{-}"
but it allows nothing.
I have seen recently that if I input hyphen then my program does not understand as "hyphen". This is a reason why your all masks did not work. Nevertheless, they are all correct. Thanks a lot for your help. Now I will find how to read hyphen as "-" in WPF 2010.
Upvotes: 2
Views: 1563
Reputation: 17268
I assume you are testing phone or fax numbers consisting of only digits and hyphens.
You may try [\d\-]+
here. Note: to match a single digit or hyphen remove +
.
Upvotes: 1
Reputation: 20267
Hypens (-
) do not act like range specifiers at the beginning of a character set, so you can do this with the regex
@"[-0-9]"
or if, like in most regex engines, WPF allows character set shorthands in character sets, you can also use
@"[-\d]"
or, as the other answers mentioned, you can escape the -
to put it in the regex, as in either of these two regular expressions
@"[0-9\-]"
@"[\d\-]"
Upvotes: 3
Reputation: 183251
I'm not familiar with WPF 2010, but if it's like most other regex engines, you can write:
@"[0-9-]"
Upvotes: 1