Python — check if a string contains Cyrillic characters

前端 未结 4 1919
一整个雨季
一整个雨季 2020-12-31 06:19

How to check whether a string contains Cyrillic characters?

E.g.

>>> has_cyrillic(\'Hello, world!\')
False
>>> has_cyrillic(\'Приве         


        
4条回答
  •  孤城傲影
    2020-12-31 07:15

    You can use a regular expression to check if a string contains characters in the а-я, А-Я range:

    import re 
    
    def has_cyrillic(text):
        return bool(re.search('[а-яА-Я]', text))
    

    Alternatively, you can match the whole Cyrillic script range:

    def has_cyrillic(text):
        return bool(re.search('[\u0400-\u04FF]', text))
    

    This will also match letters of the extended Cyrillic alphabet (e.g. ё, Є, ў).

提交回复
热议问题