正则表达式仅匹配字母 [英] Regex to match only letters

查看:137
本文介绍了正则表达式仅匹配字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写仅与字母匹配的正则表达式?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆