2

Russian alphabet includes the letter ё, which was undeservedly forgotten at beggining of computing.

So, if i want to use a regexp with character diapason, I must mention this letter separately:

[а-яА-яёЁ]

instead of:

[а-яА-Я]

example:

lets we have string "Верёвочка - 12" and need to parse only word by regular expression:

word = "Верёвочка"[/а-яА-Я/]   # => ""
word = "Верёвочка"[/а-яА-ЯёЁ/] # => "Верёвочка"

How can I upgrade regexp class in Ruby or Ruby on Rails to resolve this problem?

10
  • Cannot parse your sentence "instead simplest".
    – sawa
    Commented Jun 29, 2014 at 7:20
  • 2
    @sawa: "instead" -> "instead of". The question is about "including" a character into the range а-я by default.
    – Blender
    Commented Jun 29, 2014 at 7:26
  • 3
    I'm not a Rubyist, but Unicode has character classes that include letters in many languages but not space/punctuation--\p{Word} is one I see suggested out there. That will match not only [а-я] but [a-z], etc. too.
    – twotwotwo
    Commented Jun 29, 2014 at 7:33
  • 1
    @twotwotwo You seem to be right. This looks like an X/Y problem.
    – sawa
    Commented Jun 29, 2014 at 7:41
  • 2
    /[[:alpha:]]+/ would work, too
    – Stefan
    Commented Jun 29, 2014 at 8:17

3 Answers 3

2

The original /а-яА-Я/ and /а-яА-ЯёЁ/ patterns just match sequences of literal chars, а-яА-Я and а-яА-ЯёЁ strings respectively, since the char ranges are not enclosed with [ and ] that would form a character class. Even if they were, without a quantifier, that would only match a single char that falls within the range(s).

To match a sequence of one or more Russian letters, you need either of:

/[а-яА-ЯёЁ]+/
/[а-яё]+/i

See the Rubular demo

Note that there is NO Unicode category class like \p{Russian}, and \p{Cyrillic} matches all Cyrillic chars, not just the Russian ones. The letter Ёё does not fall into the range between а-я and А-Я and **must be added "manually", see the Unicode table:

enter image description here

And here is the Ruby demo:

s = "Верёвочка - 12"
puts s[/[а-яА-ЯёЁ]+/] # => Верёвочка
puts s[/[а-яё]+/i]    # => Верёвочка
1
  • Commenting to add that /[ЁА-яё]+/ follows Unicode code point order, which makes it very nice for (Unicode-aware) programmers to work with.
    – haley
    Commented Dec 3, 2023 at 5:15
0

This is cool - I had never thought that much about character ranges in unicode.

It seems that for some reason А-я were encoded in the unicode range 0x410 to 0x44f, but some other characters (such as ё) were added in 0x400 to 0x410 and then 0x450 to 0x45f (wikipedia has a full breakdown of what characters went where)

As a consequence, /[Ѐ-ё]/ should work, but might feel quite illogical to a native speaker.

You can of course do raw unicode escapes, i.e. /[\u0400-\u045f]/ (or up until \u04ff if you want the full cyrillic block) but that does make you either remember that (or assign it to some constant for future use).

Lastly, you can refer to entire scripts with

/\p{Cyrillic}/

although my understanding is that this includes more characters, such as Ԧ

3
  • Yes, i seek a human readable decision, like[а-я]. /\p{Cyryllic}/ not working in Ruby: SyntaxError: (pry):2: invalid character property name {Cyrillic}: /\p{Cyrillic}/ Commented Jun 29, 2014 at 8:48
  • Worked for me (ruby 2.0.0). The regexp engine in ruby has changed quite a bit between versions. Commented Jun 29, 2014 at 8:50
  • 1
    It's also includes some non russian characters like Ѐ
    – zishe
    Commented Jun 29, 2014 at 10:25
0

Is one, but not are beatifull decision: use [/а-ё/] instead of [/а-яё/]. This worked, but letter not in proper direction:

str = "верёвочка"
str[/^[а-ё]+$/]
#=> "верёвочка"
3
  • 2
    This solution is not right. Have a look at my answer to see (I posted a table) where it goes wrong. Commented Sep 20, 2017 at 12:01
  • Can't find anything. Commented Sep 21, 2017 at 21:26
  • 2
    Not sure what you mean. My solution is the correct one to find any chunk of 1+ Russian letters. Your /^[а-ё]+$/ only finds a match if the whole line consists of 1 or more chars that are one of абвгдежзийклмнопрстуфхцчшщъыьэюяѐё letters. Note that ѐ is not a Russian letter, and that regex of yours does not even match uppercase Russian letters. Commented Sep 21, 2017 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.