Ruby-on-rails 如何将重音字符与正则表达式匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7292395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to match accented characters with a regex?
提问by user502052
I am running Ruby on Rails 3.0.10 and Ruby 1.9.2. I am using the following Regex in order to match names:
我正在运行 Ruby on Rails 3.0.10 和 Ruby 1.9.2。我正在使用以下正则表达式来匹配名称:
NAME_REGEX = /^[\w\s'"\-_&@!?()\[\]-]*$/u
validates :name,
:presence => true,
:format => {
:with => NAME_REGEX,
:message => "format is invalid"
}
However, if I try to save some words like the followings:
但是,如果我尝试保存一些如下的单词:
Oilalà
Pì
Rùby
...
# In few words, those with accented characters
I have a validation error "Name format is invalid..
我有一个验证错误"Name format is invalid.。
How can I change the above Regex so to match also accented characters like à, è, é, ì, ò, ù, ...?
如何更改上述正则表达式以匹配重音字符,如à, è, é, ì, ò, ù, ...?
回答by Lars Haugseth
Instead of \w, use the POSIX bracket expression[:alpha:]:
代替\w,使用POSIX 括号表达式[:alpha:]:
"bl?b?r dèjá vu".scan /[[:alpha:]]+/ # => ["bl?b?r", "dèjá", "vu"]
"bl?b?r dèjá vu".scan /\w+/ # => ["bl", "b", "r", "d", "j", "vu"]
In your particular case, change the regex to this:
在您的特定情况下,将正则表达式更改为:
NAME_REGEX = /^[[:alpha:]\s'"\-_&@!?()\[\]-]*$/u
This does match much more than just accented characters, though. Which is a good thing. Make sure you read this blog entryabout common misconceptions regarding names in software applications.
不过,这确实不仅仅匹配重音字符。这是一件好事。请务必阅读此博客条目,了解有关软件应用程序中名称的常见误解。
回答by Andreas
One solution would of course be to simply find all of them just use them as you normally would, although I assume they can be fairly many.
一种解决方案当然是简单地找到所有这些,只是像往常一样使用它们,尽管我认为它们可能相当多。
If you are using UTF8 then you will find that such characters are often split into two parts, the "base" character itself, followed by the accent (0x0300 and 0x0301 I believe) also called a combining character. However, this may not always be true since some characters can also be written using the "hardcoded" character code... so you need to normalize the UTF8 string to NFD form first.
如果您使用的是 UTF8,那么您会发现这些字符通常分为两部分,“基本”字符本身,然后是重音符号(我相信是 0x0300 和 0x0301),也称为组合字符。但是,这可能并不总是正确的,因为某些字符也可以使用“硬编码”字符代码编写……因此您需要先将 UTF8 字符串规范化为 NFD 形式。
Of course, you could also turn any string you have into UTF8 and then back into the original charset... but the overhead might become quite large if you are doing bulk operations.
当然,您也可以将您拥有的任何字符串转换为 UTF8,然后再转换回原始字符集……但是如果您进行批量操作,开销可能会变得非常大。
EDIT: To answer your question specifically, the best solution is likely to normalize your strings into UTF8 NPD form, and then simply add 0x0300 and 0x0301 to your list of acceptable characters, and whatever other combining characters you want to allow (such as the dots in ???, you can find them all in "charmap" in Windows, look at 0x0300 and "up").
编辑:要具体回答您的问题,最好的解决方案可能是将您的字符串规范化为 UTF8 NPD 形式,然后只需将 0x0300 和 0x0301 添加到您的可接受字符列表中,以及您想要允许的任何其他组合字符(例如点在 ??? 中,您可以在 Windows 的“charmap”中找到它们,查看 0x0300 和“up”)。

