简体   繁体   English

C#.net中的正则表达式,用于字母和可选的hypen

[英]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 我需要.NET中的正则表达式来验证带有字母和选项连字符的单词,不允许使用其他特殊字符或数字

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] ). 在字符类[-a-zA-Z][a-zA-Z-]的开头或结尾处都添加“-”字符(当显示为第一个或最后一个字符时,不会被识别为字符范围)或以转义形式放在字符范围之外(例如[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]" ). (请注意,在后一种情况下,除非使用@前缀的字符串,否则将需要转义转义字符本身。即@"[az\\-AZ]""[az\\\\-AZ]" ) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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