Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python punctuation regex doesn't seem to work

Tags:

python

regex

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)
like image 927
shooqie Avatar asked Apr 07 '26 21:04

shooqie


1 Answers

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.

like image 68
jfs Avatar answered Apr 10 '26 11:04

jfs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!