You must create a regular expression to check for the presence of Cyrillic characters in the string. How can this be implemented?
- ru.stackoverflow.com/q/440125/10105 - VladD
2 answers
The question of checking whether the Cyrillic characters (the Cyrillic word is a loose term) consists of a string or something else is not completely clear. But at the core of the question, an expression like [а-яА-Я]+ will return true to you if there is at least one inclusion in the string listed characters.
Well, from myself I will supplement the answer by checking the presence of Cyrillic characters in the string:
public boolean isCyrillic(String s) { boolean result = false; for (char a : s.toCharArray()) { if (Character.UnicodeBlock.of(a) == Character.UnicodeBlock.CYRILLIC) { result = !result; break; } } return result; } UPD : to answer @Aleksei Chibisov Yes, will not return. Actually to this and the remark "Cyrillic notion of loose" - the characters themselves that belong to this group mass. The Cyrillic alphabets include: Belarusian, Bulgarian, Serbian, Macedonian, Russian, Ukrainian ... and such characters as є iw ... and a lot of others ... As for me, I should use UnicodeBlock in this case - there are extensions A and B c version 1.7. And the reaction of the method presented above to the symbol 'ё' or 'є' will be true
To work with regular expressions in Java, there are classes Pattern and Matcher:
String regex = "[а-яёА-ЯЁ]+"; String str = "Работа не walk - работа work"; Pattern pattern = Pattern.compile(regex); Matcher m = pattern.matcher(str); if (m.find()){ //делаем что-то } I will add to the previous answer that [a-YaA-Z] + does not include the symbols ё and Ё
- oneYes, you are right, I just had to put this in the comment in your answer. [A-yaoA-yayo] is only a complete set of characters of the Russian alphabet, and not the whole Cyrillic alphabet - Aleksei Chibisov