问题
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