只允许字母和"专用"字母(EEA等),通过一个正则表达式 [英] Allow only letters and "special" letters (éèà etc.) through a regex

查看:84
本文介绍了只允许字母和"专用"字母(EEA等),通过一个正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配的字符串,看它是否只包含了字母。各类信件应该被允许。因此,典型的 A-ZA-Z ,也áàéèó... 等。

I am trying to match a string to see if it only consists out of letters. All kinds of letters should be allowed. So the typical a-zA-Z, but also áàéèó... etc.

我试着用下面的正则表达式匹配它:([\S])*

I tried to match it with the following regex: ([\S])*

但是,这也让像 \ /<字符;> *()... 等,这些都是很明显的不一个名字属于字符。如何在正则表达式的样子时,我只希望允许字母和特别的信吗?

But this also allows characters like \/<>*()... etc. Those are obviously characters that don't belong in a name. How does the regex looks like when i only want to allow letters and 'special' letters?

推荐答案

您可以使用字符类,说正是:

You can use the character class that says exactly that:

\p{L}

所以,正则表达式

^\p{L}+$

将如果字符串只包含字母相匹配。如果你期望组合字符,然后

will match if the string consists only of letters. If you expect combining characters, then

^(\p{L}\p{M}*)+$

工作

快速PowerShell的测试:

Quick PowerShell test:

PS> 'foo','bär','a.b','&^#&%','123','кошка' -match '^\p{L}+$'
foo
bär
кошка

这篇关于只允许字母和&QUOT;专用&QUOT;字母(EEA等),通过一个正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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