sed to python: replace Q

Lutz Horn lutz.georg.horn at googlemail.com
Wed Apr 30 08:37:18 CEST 2008


Hi,

2008/4/30 Raymond <not-for-mail at sonic.net>:
> For some reason I'm unable to grok Python's string.replace() function.

replace() does not work with regular expressions.

>  Is there a decent description of string.replace() somewhere?

Use re.sub().

>>> import re
>>> line = "date process text [ip] more text"
>>> re.sub('].*$', '', re.sub('^.*\[', '', line, 1))
'ip'

Lutz



More information about the Python-list mailing list