简体   繁体   中英

Rails gsub for russian symbols

In my rails app i need to format my string so that it consist only letters, without symbols. But main trouble is that string is in russian language, so how do it? For rnglish and letters and digits i do that:

ArtLookup.get_analog(@articles.ART_ARTICLE_NR.gsub(/[^0-9A-Za-z]/, ''))

But how to do it for russian alphabet? (first is А, last is Я). Only letters, and delete spaces?

Use \\p{Cyrillic} , which matches any cyrillic character.

Example:

1.9.3p194 :001 > s = "helloЯ"
 => "helloЯ" 
1.9.3p194 :002 > s.gsub(/\p{Cyrillic}/, '')
 => "hello"

More info on special characters handling in Ruby: http://ruby-doc.org/core-1.9.3/Regexp.html

Edited Answer:

If you want only a subset of the cyrillic alphabet, I'm afraid you have to build your own set.

For this, you can try to use a range: /[а-я]+/i , which should work. If it doesn't, just specify your alphabet explicitely: /[абвгдеёжзийклмнопрстуфхцчшщъыьэюя]+/i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM