正则表达式只匹配字母 [英] Regex to match only letters
问题描述
如何编写仅匹配字母的正则表达式?
How can I write a regex that matches only letters?
推荐答案
使用字符集:[a-zA-Z]
以小写和大写形式匹配 A–Z 中的一个字母.[a-zA-Z]+
匹配一个或多个字母,^[a-zA-Z]+$
只匹配仅由一个或多个字母组成的字符串(^
和 $
分别标记字符串的开始和结束).
Use a character set: [a-zA-Z]
matches one letter from A–Z in lowercase and uppercase. [a-zA-Z]+
matches one or more letters and ^[a-zA-Z]+$
matches only strings that consist of one or more letters only (^
and $
mark the begin and end of a string respectively).
如果你想匹配除 A-Z 之外的其他字母,你可以将它们添加到字符集中:[a-zA-ZäöüßÄÖÜ]
.或者您使用预定义的字符类,例如 Unicode 字符属性 类 p{L}
描述作为字母的 Unicode 字符.
If you want to match other letters than A–Z, you can either add them to the character set: [a-zA-ZäöüßÄÖÜ]
. Or you use predefined character classes like the Unicode character property class p{L}
that describes the Unicode characters that are letters.
这篇关于正则表达式只匹配字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!