我想拆分一个句子将单词转换成标签(在 Mongodb
中进行简单的全文搜索),我不想保存逗号或冒号:
phrase = "hello, this is a simple description!"
pattern = "[\"\'\!\?\:\,\;]"
我已经试过了:
re.split(pattern, phrase)
Out[1]: ['hello', ' this is a simple description', ''] # as you can see, i've always blank characters.
我想删除所有“非字母字符”,有 phrase.replace(",", "")
但只替换一个字符,那么我如何使用正则表达式替换? sssomething like re.remove(pattern, phrase)
,有没有循环,这对服务器来说是不是很繁重的工作?
最佳答案
非正则表达式
解决方案:
使用 strip()
,但您需要将所有非字母字符传递给它。
类似于:strip(',!*&^%#$;:+')
In [12]: phrase = "hello, this is: a simple; description!!"
In [13]: [x.strip(',!*&^%#$;:+') for x in phrase.split()]
Out[13]: ['hello', 'this', 'is', 'a', 'simple', 'description']
关于python - 如何拆分句子,只让 ascii 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12439076/