« Return to Thread: sed to python: replace Q

Re: sed to python: replace Q

by lutz :: Rate this Message:

Reply to Author | View in Thread

Hi,

2008/4/30 Raymond <not-for-mail@...>:
> 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
--
http://mail.python.org/mailman/listinfo/python-list

 « Return to Thread: sed to python: replace Q