用正则表达式删除标点符号-python

我需要使用正则表达式在单词的 开头结尾 处删除标点符号。似乎正则表达式将是最好的选择。我不想从“

you’re”之类的词中删除标点符号,这就是为什么我不使用.replace()的原因。

回答:

您不需要正则表达式即可执行此任务。使用str.stripstring.punctuation

>>> import string

>>> string.punctuation

'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

>>> '!Hello.'.strip(string.punctuation)

'Hello'

>>> ' '.join(word.strip(string.punctuation) for word in "Hello, world. I'm a boy, you're a girl.".split())

"Hello world I'm a boy you're a girl"

以上是 用正则表达式删除标点符号-python 的全部内容, 来源链接: utcz.com/qa/403169.html

回到顶部