可以使用正则表达式 [\w\u4e00-\u9fa5]
匹配字母和汉字。
其中,\w
匹配字母、数字、下划线,\u4e00-\u9fa5
匹配汉字。
在 Python 中使用正则表达式可以使用 re
模块。
示例代码:
import re
text = "Hello, 世界"
match = re.findall(r'[\w\u4e00-\u9fa5]', text)
print(match)
输出:
['H', 'e', 'l', 'l', 'o', '世', '界']