正则匹配中文
可以使用 Unicode 编码来匹配中文字符。
比如,匹配中文字符可以使用正则表达式 \p{Han}。
示例代码:
import re
text = "这是一段中文文本。"
pattern = re.compile(r'\p{Han}')
matches = pattern.findall(text)
print(matches) # ['这', '是', '一', '段', '中', '文', '文', '本']
在这个例子中,使用了 \p{Han} 表示匹配所有汉字。可以通过 findall 方法来找到所有匹配的字符串。
原文地址: https://www.cveoy.top/t/topic/g0HJ 著作权归作者所有。请勿转载和采集!