如何将重音字符与正则表达式匹配? [英] How to match accented characters with a regex?

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

问题描述

我正在运行 Ruby on Rails 3.0.10 和 Ruby 1.9.2.我正在使用以下正则表达式来匹配名称:

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:

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

我有一个验证错误名称格式无效..

如何更改上述正则表达式以匹配重音字符,例如 àèéì, ò, ù, ...?

How can I change the above Regex so to match also accented characters like à, è, é, ì, ò, ù, ...?

推荐答案

代替 \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 entry about common misconceptions regarding names in software applications.

这篇关于如何将重音字符与正则表达式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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