473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

sed to python: replace Q

For some reason I'm unable to grok Python's string.replace() function.
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/].*$//'

yet the following Python code does nothing:

line = line.replace('^.*\[', '', 1)
line = line.replace('].*$', '')

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

Raymond
Jun 27 '08 #1
10 3534
Hi,

2008/4/30 Raymond <no**********@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
Jun 27 '08 #2
On Apr 29, 11:27*pm, Raymond <not-for-m...@sonic.netwrote:
For some reason I'm unable to grok Python's string.replace() function.
line = "abc"
line = line.replace("a", "x")
print line

--output:--
xbc

line = "abc"
line = line.replace("[apq]", "x")
print line

--output:--
abc
Does the 5 character substring "[apq]" exist anywhere in the original
string?
Jun 27 '08 #3
Raymond wrote:
For some reason I'm unable to grok Python's string.replace() function.
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/].*$//'
alternatively:
sed -e 's/.*\[\(.*\)].*/\1/'
yet the following Python code does nothing:

line = line.replace('^.*\[', '', 1)
line = line.replace('].*$', '')

Is there a decent description of string.replace() somewhere?
In python shell:
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
Jun 27 '08 #4
On Wed, 30 Apr 2008 15:27:36 +1000, Raymond <no**********@sonic.netwrote:
For some reason I'm unable to grok Python's string.replace() function.
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/].*$//'

yet the following Python code does nothing:

line = line.replace('^.*\[', '', 1)
line = line.replace('].*$', '')
str.replace() doesn't support regular expressions.

Try:

import re
p = re.compile("^.*\[")
q = re.compile("].*$")
q.sub('',p.sub('', line))
>
Is there a decent description of string.replace() somewhere?

Raymond
Section 3.6.1 String Functions

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Jun 27 '08 #5
On Wed, 30 Apr 2008 17:12:15 +1000, Kam-Hung Soh <ka*********@gmail.com>
wrote:
On Wed, 30 Apr 2008 15:27:36 +1000, Raymond <no**********@sonic.net
wrote:
>For some reason I'm unable to grok Python's string.replace() function..
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/].*$//'

yet the following Python code does nothing:

line = line.replace('^.*\[', '', 1)
line = line.replace('].*$', '')

str.replace() doesn't support regular expressions.

Try:

import re
p = re.compile("^.*\[")
q = re.compile("].*$")
q.sub('',p.sub('', line))
Another approach is to use the split() function in "re" module.

import re
re.split("[\[\]]", line)[1]

See http://docs.python.org/lib/node46.html

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Jun 27 '08 #6
>Another approach is to use the split() function in "re" module.

Ah ha, thar's the disconnect. Thanks for all the pointers, my def is
now working. Still don't understand the logic behind this design though.
I mean why would any programming language have separate search or find
functions, one for regex and and another for non-regex based pattern
matching?

Aren't sed, awk, grep, and perl the reference implementations of search
and replace? They don't have non-regex functions, why does Python?
Wouldn't it be a lot simpler to use a flag, like grep's '-f', to change
the meaning of a search string to be literal?

My other gripe is with the kludgy object-oriented regex functions.
Couldn't these be better implemented in-line? Why should I, as a coder,
have to 're.compile()' when all the reference languages do this at compile
time, from a much more straightforward and easy to read in-line function...

Raymon
Jun 27 '08 #7
Raymond wrote:
Aren't sed, awk, grep, and perl the reference implementations of search
and replace?
I don't know about "reference implementations", but I daresay they are a
mess w.r.t. usability.

Jun 27 '08 #8
Mel
Raymond wrote:
My other gripe is with the kludgy object-oriented regex functions.
Couldn't these be better implemented in-line? Why should I, as a coder,
have to 're.compile()' when all the reference languages do this at compile
time, from a much more straightforward and easy to read in-line
function...
Because compile time doesn't do

pattern = raw_input ("Pattern, please: ")
saved_pattern = re.compile (pattern)

Mel.

Jun 27 '08 #9
Ah ha, thar's the disconnect. Thanks for all the pointers, my def is
now working. Still don't understand the logic behind this design though.
I mean why would any programming language have separate search or find
functions, one for regex and and another for non-regex based pattern
matching?

Aren't sed, awk, grep, and perl the reference implementations of search
and replace? They don't have non-regex functions, why does Python?
Wouldn't it be a lot simpler to use a flag, like grep's '-f', to change
the meaning of a search string to be literal?
And by this possibly destroying other modules code that rely on their
respective strings being that - and not patterns.
My other gripe is with the kludgy object-oriented regex functions.
Couldn't these be better implemented in-line? Why should I, as a coder,
have to 're.compile()' when all the reference languages do this at compile
time, from a much more straightforward and easy to read in-line
function...
You can do that already, no need to - the patterns are cached. Albeit the
cache might be limited in size. but code like

m = re.match(pattern, s)

is not considerably slower than

rex = re.compile(pattern)
m = rex.match(s)

Diez
Jun 27 '08 #10
On Tue, 06 May 2008 14:55:07 +0000, Raymond wrote:
>>Another approach is to use the split() function in "re" module.

Ah ha, thar's the disconnect. Thanks for all the pointers, my def is
now working. Still don't understand the logic behind this design
though. I mean why would any programming language have separate search
or find functions, one for regex and and another for non-regex based
pattern matching?

Aren't sed, awk, grep, and perl the reference implementations of search
and replace? They don't have non-regex functions, why does Python?
Wouldn't it be a lot simpler to use a flag, like grep's '-f', to change
the meaning of a search string to be literal?

My other gripe is with the kludgy object-oriented regex functions.
Couldn't these be better implemented in-line? Why should I, as a coder,
have to 're.compile()' when all the reference languages do this at
compile time, from a much more straightforward and easy to read in-line
function...

Raymon
Hm. Are regex's first class citizens in these languages, like they are
in python?

And from a language design perspective, isn't it much cleaner to put
regex's into just another portion of the runtime rather than dumping it
into the language definition proper?

It does actually make sense - to have a string method do a string thing,
and to have a regex method do a regex thing. And while command line
options are pretty nice when done well, there's nothing in particular
stopping one from using arguments with defaults in python.

I'm good with sed and grep, though I never got into awk much - perhaps a
small mistake. When it came to perl, I skipped it and went directly to
python, and have never regretted the decision. Python's got a much more
coherent design than perl, most certainly, and more than sed as well.
awk's not that bad though. And grep's nice and focused - I quite like
grep's design.
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 404 open ( +2) / 3376 closed (+16) / 3780 total (+18) Bugs : 860 open ( -1) / 6131 closed (+17) / 6991 total (+16) RFE : 229 open...
122
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection...
13
by: Michael M. | last post by:
In Perl, it was: ## Example: "Abc | def | ghi | jkl" ## -"Abc ghi jkl" ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st pipe). $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;...
5
by: Johann C. Rocholl | last post by:
The following is my first attempt at adding a taint feature to Python to prevent os.system() from being called with untrusted input. What do you think of it? # taint.py - Emulate Perl's taint...
3
by: koutoo | last post by:
Is it possible to display messages in the python shell? I want to display error messages based on parameters in my scripts to the users. Is there another way to display messages other than log...
3
by: Phillip B Oldham | last post by:
Hi. I'm stretching my boundaries in programming with a little python shell-script which is going to loop through a list of domain names, grab the whois record, parse it, and put the results into a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.