Home > Enterprise >  Regex to find at least one cyrillic character
Regex to find at least one cyrillic character

Time:02-15

I want to find one or more cyrillic characters(а-я) within a string. So far i manage to find the character wherever it is except for the start of the string.

Expression that i'am using -> ^[\p{L}\d\s\-](.*[а-яА-Я].*) $

  1. ok -> loremфффф ф
  2. ok -> ipsuфmл
  3. ok -> ffffл
  4. ok -> фgfфdфg
  5. ок -> ллlorem
  6. fail -> лlorem (because the first letter is in cyrillic and it is the only one)

https://regex101.com/

CodePudding user response:

You can use

^\P{Cyrillic}*\p{Cyrillic}.*

See the regex demo.

If you want to only deal with Russian chars, you can replace \p{Cyrillic} with [а-яёА-ЯЁ] and \P{Cyrillic} with [^а-яёА-ЯЁ].

Details:

  • ^ - start of string
  • \P{Cyrillic}* - zero or more chars other than Cyrillic
  • \p{Cyrillic} - a Cyrillic char
  • .* - zero or more chars other than line break chars as many as possible.

To match multiline strings, add (?s) at the start, or replace . with a [\w\W] workaround construct.

  • Related