我今天需要从一堆字符串中去除中文,并且正在寻找一个简单的 Python 正则表达式。有什么建议吗?
最佳答案
Python 2:
#!/usr/bin/env python
# -*- encoding: utf8 -*-
import re
sample = u'I am from 美国。We should be friends. 朋友。'
for n in re.findall(ur'[\u4e00-\u9fff]+',sample):
print n
Python 3:
sample = 'I am from 美国。We should be friends. 朋友。'
for n in re.findall(r'[\u4e00-\u9fff]+', sample):
print(n)
输出:
美国
朋友
4E00—9FFF
范围涵盖 CJK Unified Ideographs (CJK=中文、日文和韩文)。在某种程度上,有许多较低的范围与 CJK 相关:
31C0—31EF CJK Strokes
31F0—31FF Katakana Phonetic Extensions
3200—32FF Enclosed CJK Letters and Months
3300—33FF CJK Compatibility
3400—4DBF CJK Unified Ideographs Extension A
4DC0—4DFF Yijing Hexagram Symbols
4E00—9FFF CJK Unified Ideographs
关于python - 使用 Python 和正则表达式查找字符串中的所有中文文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2718196/