Re: sed to python: replace Q
- From: Robert Bossy <Robert.Bossy@xxxxxxxxxxxx>
- Date: Wed, 30 Apr 2008 09:11:35 +0200
Raymond wrote:
For some reason I'm unable to grok Python's string.replace() function.alternatively:
Just trying to parse a simple IP address, wrapped in square brackets,
from Postfix logs. In sed this is straightforward given:
line = "date process text [ip] more text"
sed -e 's/^.*\[//' -e 's/].*$//'
sed -e 's/.*\[\(.*\)].*/\1/'
yet the following Python code does nothing:In python shell:
line = line.replace('^.*\[', '', 1)
line = line.replace('].*$', '')
Is there a decent description of string.replace() somewhere?
help(str.replace)
Online:
http://docs.python.org/lib/string-methods.html#l2h-255
But what you are probably looking for is re.sub():
http://docs.python.org/lib/node46.html#l2h-405
RB
.
- References:
- sed to python: replace Q
- From: Raymond
- sed to python: replace Q
- Prev by Date: Re: computing with characters
- Next by Date: Re: sed to python: replace Q
- Previous by thread: Re: sed to python: replace Q
- Next by thread: Re: sed to python: replace Q
- Index(es):
Relevant Pages
|