Python — check if a string contains Cyrillic characters

依然范特西╮ 提交于 2019-12-30 04:21:24

问题


How to check whether a string contains Cyrillic characters?

E.g.

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

回答1:


regex supports Unicode properties, along with a few short forms.

>>> regex.search(r'\p{IsCyrillic}', 'Hello, world!')
>>> regex.search(r'\p{IsCyrillic}', 'Привет, world!')
<regex.Match object; span=(0, 1), match='П'>
>>> regex.search(r'\p{IsCyrillic}', 'Hello, wёrld!')
<regex.Match object; span=(8, 9), match='ё'>



回答2:


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. ё, Є, ў).




回答3:


You could create a set containing the cyrillic letters and just check each character of the string:

cyrillic_letters = {....} # fill it with the cyrillic letters

def has_cyrillic(text):
    for c in text:
        if c in cyrillic_letters:
            return True
    return False


来源:https://stackoverflow.com/questions/48255244/python-check-if-a-string-contains-cyrillic-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!