开发者

Regular Expression in C#.net for alphabets and optional hypen

开发者 https://www.devze.com 2023-02-16 16:23 出处:网络
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

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]").

0

精彩评论

暂无评论...
验证码 换一张
取 消