6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyで基底文字+結合文字だけ正規化する

Last updated at Posted at 2018-06-28

やりかた1

str = "神" + "Äが".unicode_normalize(:nfd)
# => "神Äが"
str.size
# => 5
str2 = str.each_grapheme_cluster.map {|s| s.size > 1 ? s.unicode_normalize : s }.join
# => "神Äが"
str2.size
# => 3

やりかた2

str = "神" + "Äが".unicode_normalize(:nfd)
# => "神Äが"
str.size
# => 5
str2 = str.gsub(/\X/) {|s| s.size > 1 ? s.unicode_normalize : s }
# => "神Äが"
str2.size
# => 3

だめなやりかた

が正規化されてになっている

str = "神" + "Äが".unicode_normalize(:nfd)
# => "神Äが"
str.size
# => 5
str2 = str.unicode_normalize
# => "神Äが"
str2.size
# => 3
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?