开发者

Regular Expression to match only letters

开发者 https://www.devze.com 2023-01-16 05:48 出处:网络
I need write a regular expression for RegularExpressionValidator ASP.NET Web Controls. The regular expression should ALLOW all alphabetic characters but not numbers or special characters (example: |!

I need write a regular expression for RegularExpressionValidator ASP.NET Web Controls.

The regular expression should ALLOW all alphabetic characters but not numbers or special characters (example: |!"£$%&/().

A开发者_运维技巧ny idea how to do it?


^[A-Za-z]+$

validates a string of length 1 or greater, consisting only of ASCII letters.

^[^\W\d_]+$

does the same for international letters, too.

Explanation:

[^   # match any character that is NOT a
\W   # non-alphanumeric character (letters, digits, underscore)
\d   # digit
_    # or underscore
]    # end of character class

Effectively, you get \w minus (\d and _).

Or, you could use the fact that ASP.NET supports Unicode properties:

^\p{L}+$

validates a string of Unicode letters of length 1 or more.


Including spaces:

"^[a-zA-Z ]*$"

Excluding Spaces:

"^[a-zA-Z]*$"

To make it non-optional, change the * to a +


You can use the regex:

^[a-zA-Z]+$

Explanation:

  • ^ : Start anchor
  • [..] : Char class
  • + : one or more repetations
  • $ : End anchor
0

精彩评论

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