简体   繁体   中英

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 numbers is not allowed

eg

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 (eg [az\\-AZ] ).

(Note that, in the latter case, unless you use a @-prefixed string, you will need to escape the escape character itself. Ie either @"[az\\-AZ]" or "[az\\\\-AZ]" ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM