How can I match Korean characters in a Ruby regular expression?
cjk, regex, ruby, ruby-1.9, unicode
Solution
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.
Problem
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. 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 `안녕[]`. I need to do this in Ruby 1.9.