开发者

How do I use a Ruby regex to capture non-English words?

开发者 https://www.devze.com 2023-03-10 18:57 出处:网络
开发者_运维技巧I am trying to validate \'words\' with Ruby 1.8.7. My regex to catch a word is currently:
开发者_运维技巧

I am trying to validate 'words' with Ruby 1.8.7.

My regex to catch a word is currently:

/[a-zA-Z]\'*\-*/

This will only catch English words; Is there a way to catch non-English UTF-8 characters?


Even the 1.8.x Regex engine is UTF-8 aware, you just need to use the right expression, and it's slightly more than just using /\w/:

s = "résumé and some other words"
puts s[/[a-z]+/u]
puts s[/\w+/u]

and you get:

r
résumé
0

精彩评论

暂无评论...
验证码 换一张
取 消