python提取字符串中所有中文
要使用正则表达式在Python中匹配文本中的所有中文字符,你可以使用Unicode编码范围来定义中文字符的模式。下面是一个示例代码:
import re
def extract_chinese(text):
pattern = re.compile(r'[\u4e00-\u9fa5]+')
chinese_chars = re.findall(pattern, text)
return chinese_chars
# 示例用法
text = 'Hello 你好,世界!'
chinese_chars = extract_chinese(text)
print(chinese_chars)
输出:
['你好', '世界']
评论已关闭