如何在Ruby正则表达式中匹配韩文字符? [英] How can I match Korean characters in a Ruby regular expression?

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

问题描述

我对使用正则表达式的用户名进行了一些基本验证,例如 [\w -_] + ,我想添加对韩文字母的支持,同时

I have some basic validations for usernames using regular expressions, something like [\w-_]+, and I want to add support for Korean alphabet, while still keeping the validation the same.

我不想允许特殊字符,例如 {} []!@#$%^& ; *()等,我只想将 \w 替换为与给定字母匹配的内容,除了 [a-zA-Z0-9]

I don't want to allow special characters, such as {}[]!@#$%^&*() etc., I just want to replace the \w with something that matches a given alphabet in addition to [a-zA-Z0-9].

这表示用户名,例如안녕应该是有效的,但不是안녕[]

Which means username like 안녕 should be valid, but not 안녕[].

我需要在Ruby 1.9中做到这一点。 / p>

I need to do this in Ruby 1.9.

推荐答案

您可以测试以下无效字符:

You can test for invalid characters like this:

#encoding: utf-8
def valid_name?(name)
  !name.match(/[^a-zA-Z0-9\p{Hangul}]/)
end

ar = %w(안녕 name 안녕[].)
ar.each{|name| puts "#{name} is #{valid_name?(name) ? "valid" : "invalid"}."}
# 안녕 is valid.
# name is valid.
# 안녕[]. is invalid.

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

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