Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex "\w" doesn't process utf-8 characters in Ruby 1.9.2

Regex \w doesn't match utf-8 characters in Ruby 1.9.2. Anybody faced same problem?

Example:

/[\w\s]+/u

In my rails application.rb I've added config.encoding = "utf-8"

like image 551
Alexey Zakharov Avatar asked Aug 08 '26 23:08

Alexey Zakharov


1 Answers

Define "doesn't match utf-8 characters"? If you expect \w to match anything other than exactly the uppercase and lowercase ASCII letters, the ASCII digits, and underscore, it won't -- Ruby has defined \w to be equivalent to [A-Za-z0-9_] regardless of Unicode. Maybe you want \p{Word} or something similar instead.

Ref: Ruby 1.9 Regexp documentation (see section "Character Classes").

like image 78
hobbs Avatar answered Aug 10 '26 13:08

hobbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!