我有一个字符串数据集,有些字符串包含混合词,如下所示: سلام12World دوربینdigital سال2012good...我想要的输出是: 12 سلام world دوربین digital 2012 سال good这是我的代码: def spliteKeyWord(str): regex = r"[\u200b-\u200c]|[0-9]+|[a-zA-Z]+\'*[a-z]*" matches = re.findall(regex, str, re.UNICODE) return matches但这段代码没有显示我想要的输出。有可能得到类似的输出吗?
2 回答

一只甜甜圈
TA贡献1836条经验 获得超5个赞
您可以使用re.findall
交替模式:
def spliteKeyWord(s): return re.findall(r'[\dA-Za-z]+|[^\dA-Za-z\W]+', s, re.UNICODE)

白衣非少年
TA贡献1155条经验 获得超0个赞
参考这个问题,你可以使用这个正则表达式来解析非 ascii 字符:
words = ['12سلامWorld','دوربینdigital','2012سالgood']
for w in words:
re.split(r'([^\x00-\x7F]+)', w)
# ['12', 'سلام', 'World']
# ['', 'دوربین', 'digital']
# ['2012', 'سال', 'good']
这将拆分非 ascii 单词之间的所有内容。
添加回答
举报
0/150
提交
取消