Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match unicode words with ruby 1.9?

I'm using ruby 1.9 and trying to find out which regex I need to make this true:

Encoding.default_internal = Encoding.default_external = 'utf-8'
"föö".match(/(\w+)/u)[1] == "föö"
# => false
like image 573
Reactormonk Avatar asked Aug 26 '10 14:08

Reactormonk


2 Answers

You can manually turn on Unicode matching using the inside (?u) syntax:

"föö".match(/(?u)(\w+)/)[1] == "föö"
# => true

However, using Unicode Property Syntax (steenslag's answer) or POSIX Brackets Syntax is better style, since they both automatically respect Unicode codepoints:

"föö".match(/(\p{word}+)/)[1] == "föö"
# => true

"föö".match(/([[:word:]]+)/)[1] == "föö"
# => true

See this blog post for more info about matching Unicode characters in Ruby regexes.

like image 81
J-_-L Avatar answered Sep 28 '22 00:09

J-_-L


# encoding=utf-8 
p "föö".match(/\p{Word}+/)[0] == "föö"
like image 22
steenslag Avatar answered Sep 27 '22 23:09

steenslag