[Regexes] Stripping puctuation from a text

Andrew Lee fiacre.patrick at gmail.com
Fri May 23 03:21:09 EDT 2008


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]+')



More information about the Python-list mailing list