TJ1*_*TJ1 27 python regex non-english
我有一些混合了英文和没有英文字母的字符串.例如:
w='_1991_??_??2'
Run Code Online (Sandbox Code Playgroud)
如何使用Regex或Python中的任何其他快速方法识别这些类型的字符串?
我不希望将字符串的字母逐个与字母列表进行比较,而是一次性快速地完成.
Sal*_*ali 56
您可以检查字符串是否只能用ASCII字符(拉丁字母+其他字符)编码.如果它不能被编码,那么它具有来自其他字母表的字符.
注意评论# -*- coding: .....它应该在python文件的顶部(否则你会收到一些关于编码的错误)
# -*- coding: utf-8 -*-
def isEnglish(s):
try:
s.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return False
else:
return True
assert not isEnglish('slabiky, ale liší se podle významu')
assert isEnglish('English')
assert not isEnglish('?? ???????? ?? ?????? ??')
assert not isEnglish('how about this one : ? asf?')
assert isEnglish('?fd4))45s&')
Run Code Online (Sandbox Code Playgroud)
Tor*_*llo 22
恕我直言,这是最简单的解决方案:
def isEnglish(s):
return s.isascii()
print(isEnglish("Test"))
print(isEnglish("_1991_??_??2"))
Output:
True
False
Run Code Online (Sandbox Code Playgroud)
Kat*_*ova 11
如果您使用字符串(而不是unicode对象),您可以使用转换和检查来清除它isalnum(),这比抛出异常更好:
import string
def isEnglish(s):
return s.translate(None, string.punctuation).isalnum()
print isEnglish('slabiky, ale liší se podle významu')
print isEnglish('English')
print isEnglish('?? ???????? ?? ?????? ??')
print isEnglish('how about this one : ? asf?')
print isEnglish('?fd4))45s&')
print isEnglish('????? ?? ???????')
> False
> True
> False
> False
> True
> False
Run Code Online (Sandbox Code Playgroud)
您还可以使用此函数从字符串中过滤非ascii字符:
ascii = set(string.printable)
def remove_non_ascii(s):
return filter(lambda x: x in ascii, s)
remove_non_ascii('slabiky, ale liší se podle významu')
> slabiky, ale li se podle vznamu
Run Code Online (Sandbox Code Playgroud)
我相信这个运行时间会很短,因为一旦它发现一个不是拉丁字母的字符就会停止。它还使用生成器来更好地使用内存。
\n\nimport string\n\ndef has_only_latin_letters(name):\n char_set = string.ascii_letters\n return all((True if x in char_set else False for x in name))\n\n>>> has_only_latin_letters(\'_1991_\xd8\xa7\xd9\x81_\xd8\xac\xd9\x8a2\')\nFalse\n>>> has_only_latin_letters(\'bla bla\')\nTrue\n>>> has_only_latin_letters(\'bl\xc3\xa4 bl\xc3\xa4\')\nFalse\n>>> has_only_latin_letters(\'\xec\xa0\x80\xec\xa3\xbc\xec\xa4\x91\xec\x95\x99\xec\xb4\x88\xeb\x93\xb1\xed\x95\x99\xea\xb5\x90\')\nFalse\n>>> has_only_latin_letters(\'also a string with numbers and punctuation 1, 2, 4\')\nTrue\nRun Code Online (Sandbox Code Playgroud)\n\n您还可以使用不同的字符集:
\n\n>>> string.ascii_letters\n\'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\'\n\n>>> string.ascii_lowercase\n\'abcdefghijklmnopqrstuvwxyz\'\n\n>>> string.ascii_uppercase\n\'ABCDEFGHIJKLMNOPQRSTUVWXYZ\'\n\n>>> string.punctuation\n\'!"#$%&\\\'()*+,-./:;<=>?@[\\\\]^_`{|}~\'\n\n>>> string.digits\n\'0123456789\'\n\n>>> string.digits + string.lowercase\n\'0123456789abcdefghijklmnopqrstuvwxyz\' \n\n>>> string.printable\n\'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%& \n\\\'()*+,-./:;<=>?@[\\\\]^_`{|}~ \\t\\n\\r\\x0b\\x0c\'\nRun Code Online (Sandbox Code Playgroud)\n\n要添加拉丁重音字母,可以参考这篇文章。
\n小智 6
import re
english_check = re.compile(r'[a-z]')
if english_check.match(w):
print "english",w
else:
print "other:",w
Run Code Online (Sandbox Code Playgroud)