shooqie
shooqie

Reputation: 1022

Python punctuation regex doesn't seem to work

I'm trying to delete all punctuation from a text using regex. The problem is, punctuation regex doesn't seem to have any effect (neither \p{P} nor \p{Punct}).

import re

hello_world = 'Hello, world!'
hello_world = re.sub('\p{Punct}', '', hello_world)
print(hello_world)

Am I doing something wrong? The following produces the desired effect, but I still don't get why the code above doesn't work.

# import string

# ...

hello_world = re.sub('[{}]'.format(string.punctuation), '', hello_world)

Upvotes: 3

Views: 1127

Answers (1)

jfs
jfs

Reputation: 414179

stdlib's re module does not support specifying properties (\p{}). There is regex module that does support the properties and it is a drop-in replacement for the re module.

Upvotes: 5

Related Questions