有如下内容:
text = '<div class="comment-content comment-content_new">测试</div> <div class="comment-content comment-content_new">学习正则</div>'
使用正则 匹配出所有的中文。
- 第一种
p = re.compile(r'([^x00-xff]*)\<\/div\>')
for m in p.finditer(text):
print(m.group(1))
# 打印结果:
测试
学习正则
这样就是比较的简单,直接是 匹配 Ascii
码大于 255
的那些字符(包括中文符号)
。
- 第二种
res = re.findall(u"[\u4e00-\u9fa5]+", str(text))
print(res)
# 打印结果:
['测试', '学习正则']
\u4e00-\u9fa5
是 unicode
编码的中文编码范围,用它来匹配中文也是非常的合适。
还可以在添加一些优化,使得可以匹配出中文的字符。
text = '<div class="comment-content comment-content_new">测试,。、【】、</div> <div class="comment-content comment-content_new">学习正则</div>'
res = re.findall(u"[\u2000-\u206f\u3000-\u303f\u4e00-\u9fef\uff00-\uffef]+", str(text)
print(res)
# 打印结果:
['测试,。、【】、', '学习正则']
# http://www.unicode.org/charts/PDF/U2000.pdf 一般标点
# http://www.unicode.org/charts/PDF/U3000.pdf CJK符号和标点
# http://www.unicode.org/charts/PDF/U4E00.pdf CJK统一表意文字
# http://www.unicode.org/charts/PDF/UFF00.pdf 半宽全宽形状
"[\u2000-\u206f\u3000-\u303f\u4e00-\u9fef\uff00-\uffef]*";