• 周四. 4月 25th, 2024

python 判断字符串中是否含有汉字或非汉字的实例

城主

12月 13, 2019 ,

今天遇到python 判断字符串中是否含有汉字或非汉字的要求。以下是python 判断字符串中是否含有汉字或非汉字的代码片段。

model中compile值可以根据需要更改,满足不同的检测需求。

#判断一段文本中是否包含简体中文
importrezhmodel =re.compile(u'[\u4e00-\u9fa5]')  #检查中文
#zhmodel = re.compile(u'[^\u4e00-\u9fa5]')  #检查非中文contents =u'(2019)可图网:https://www.scielib.com/'
match =zhmodel.search(contents)
if match:
print(contents)
else:
print(u'没有包含中文')

以上就是python 判断字符串中是否含有汉字或非汉字的实例。

经过测试,上面的python代码能判断字符串中是否含有汉字。

阅读  使用 WinRAR 命令行创建分卷存档