什么都不替换标点符号

The*_*ird 0 python regex string

>>> import re
>>> a="what is. your. name? It's good"
>>> b=re.findall(r'\w+',a)
>>> b
['what', 'is', 'your', 'name', 'It', 's', 'good']
Run Code Online (Sandbox Code Playgroud)

上面的结果分裂It's['It','s']我不想要那样.

我想就什么也没有如更换它It'sIts.同样适用于所有标点符号.我怎样才能做到这一点?

Abh*_*jit 5

你被迫使用正则表达式吗?这个任务可以很容易地通过NE使用完成str.translatestring.punctuation作为deletechars

>>> from string import punctuation
>>> a="what is. your. name? It's good"
>>> a.translate(None, punctuation)
'what is your name Its good'
Run Code Online (Sandbox Code Playgroud)

如果您被迫使用正则表达式,那么另一种选择就是

>>> from string import punctuation
>>> r = re.compile(r'[{}]+'.format(re.escape(punctuation)))
>>> r.sub('', a)
'what is your name Its good'
Run Code Online (Sandbox Code Playgroud)

但是,我仍然建议你重新考虑这个设计.使用Regex执行此任务是一种过度杀伤力.

  • @aIKid:我想坚持实际的问题.`什么都不替换标点符号 (2认同)