By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

[Regexes] Stripping puctuation from a text

P: n/a
I want to strip punctuation from text.

So I am trying,
>>p = re.compile('[a-zA-Z0-9]+')
p.sub('', 'I love tomatoes!! hell yeah! ... Why?')
' !! ! ... ?'

Which gave me all the chars which I want to replace.

So Next I tried by negating the regex,
>>p = re.compile('^[a-zA-Z0-9]+')
p.sub('', 'I love tomatoes!! hell yeah! ... Why?')
' love tomatoes!! hell yeah! ... Why?'

But this removed the first char instead of the puctuation. So I guess
^ is matching start of line, instead of negation. How can I take
negation of the regex here?
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
shabda raaj wrote:
I want to strip punctuation from text.

So I am trying,
>>>p = re.compile('[a-zA-Z0-9]+')
p.sub('', 'I love tomatoes!! hell yeah! ... Why?')
' !! ! ... ?'

Which gave me all the chars which I want to replace.

So Next I tried by negating the regex,
>>>p = re.compile('^[a-zA-Z0-9]+')
p.sub('', 'I love tomatoes!! hell yeah! ... Why?')
' love tomatoes!! hell yeah! ... Why?'

But this removed the first char instead of the puctuation. So I guess
^ is matching start of line, instead of negation. How can I take
negation of the regex here?

p = re.compile('[^a-zA-Z0-9]+')
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.