Regular Expression in C#.net for alphabets and optional hypen
I need a regular expression in .NET for validating words with alphabets and option hyphen, no other special characters or n开发者_StackOverflow中文版umbers is not allowed
e.g
ASAS-JDS
ANND-Jdsd
asdasda
I got the regular expression for alphabets
^[a-zA-Z]+$
but i also need the optional hyphen "-" to be included.
Either put in the '-' character at the beginning or end of the character class [-a-zA-Z]
or [a-zA-Z-]
(when appearing as the first or last character, it's not recognized as part of a character range) or put it in there in escaped form, outside the character ranges (e.g. [a-z\-A-Z]
).
(Note that, in the latter case, unless you use a @-prefixed string, you will need to escape the escape character itself. I.e. either @"[a-z\-A-Z]"
or "[a-z\\-A-Z]"
).
精彩评论