我有一个显示名称字段,需要使用Ruby正则表达式进行验证。我们必须匹配所有语言字符,如法语、阿拉伯语、中文、德语、西班牙语以及除了特殊字符如*()!@#$%^&之外的英语字符。我不知道如何匹配那些非拉丁字符。
有两种可能性:
Create a regex with a negated character class containing every symbol you don't want to match:
if ( name ~= /[^*!@%\^]/ ) # add everything and if this matches you are good
This solution may not be feasible, since there is a massive amount of symbols you'd have to insert, even if you were just to include the most common ones.
Use Oniguruma (see also: Oniguruma for Ruby main). This supports Unicode and their properties; in which case all letters can be matched using:
if ( name ~= /[\pL\pM]/ )
You can see what these are all about here: Unicode Regular Expressions
从 Ruby 1.9 开始,String
和 Regex
类别具有 Unicode 知识。您可以安全地使用 Regex 单词字符选择器 \w
"可口可樂!?!".gsub /\w/, 'Ha'
#=> "HaHaHaHa!?!"
\w
被定义为[0-9A-Za-z_]
。 - Michael Kohl\w
在 Unicode 中匹配的不仅仅是那些字符) - edgerunner\p{L}
匹配所有语言中的单词字符(不需要像之前的答案中所描述的那样使用 Oniguruma gem)。