用正则表达式剥离标点符号 - python

use*_*287 16 python regex

我需要使用正则表达式来删除单词开头结尾的标点符号.似乎正则表达式是最好的选择.我不希望从"你是"这样的单词中删除标点符号,这就是为什么我不使用.replace().在此先感谢=)

fal*_*tru 43

您不需要正则表达式来执行此任务.使用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"
Run Code Online (Sandbox Code Playgroud)

  • 只是出于好奇,这个正则表达式方法是什么? (2认同)