469,289 Members | 2,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

Searching and replacing text ?

Hi,

I'm new to Python (and admittedly not a very good programmer) and I've
come across a scenario where I've to search and replace text in a
file.

For the sake of an example, I'm searching for every occurence of the
text
[[http://www.hotmail.com -Hotmail]]

I've to replace it with
[http://www.hotmail.com Hotmail]

I've come up with following scheme
p=re.compile(r'\[\[')
q=re.compile(r'->')

p.sub('[',txt)
q.sub('\b',txt)

Give that I don't have very strong RegEX background, this doesn't look
very elegant. Is there some other way I can accomplish the same thing?
Moreover, please note that I'm using 'p' and 'q' for two regex and
then calling 'sub()' on both p and q. Can't I just do that by
employing one RegEx and then calling sub() only once?

Please enlighten me. Thanks in advance.

Jun 27 '08 #1
3 1122
En Fri, 02 May 2008 15:51:42 -0300, Oltmans <ro**********@gmail.com>
escribió:
Hi,

I'm new to Python (and admittedly not a very good programmer) and I've
come across a scenario where I've to search and replace text in a
file.

For the sake of an example, I'm searching for every occurence of the
text
[[http://www.hotmail.com -Hotmail]]

I've to replace it with
[http://www.hotmail.com Hotmail]

I've come up with following scheme
p=re.compile(r'\[\[')
q=re.compile(r'->')

p.sub('[',txt)
q.sub('\b',txt)
Is it a constant text? Then use the replace method of string objects: no
re is needed.

text = "This is [[http://www.hotmail.com -Hotmail]] some text"
print text.replace("[[http://www.hotmail.com -Hotmail]]",
"[http://www.hotmail.com Hotmail]")
output: This is [http://www.hotmail.com Hotmail] some text

Or, do you want to find '[[' followed by some word followed by ' -'
followed by another word followed by ']]'?

r = re.compile(r'\[\[(.+?)\s*->\s*(.+?)\]\]')
print r.sub(r'[\1 \2]', text)
(same output as above)

() in the regular expression indicate groups, that you can reference as \1
\2 in the replacement string.

--
Gabriel Genellina

Jun 27 '08 #2
Oltmans wrote:
Hi,

I'm new to Python (and admittedly not a very good programmer) and I've
come across a scenario where I've to search and replace text in a
file.

For the sake of an example, I'm searching for every occurence of the
text
[[http://www.hotmail.com -Hotmail]]

I've to replace it with
[http://www.hotmail.com Hotmail]

I've come up with following scheme
p=re.compile(r'\[\[')
q=re.compile(r'->')

p.sub('[',txt)
q.sub('\b',txt)

Give that I don't have very strong RegEX background, this doesn't look
very elegant. Is there some other way I can accomplish the same thing?
Moreover, please note that I'm using 'p' and 'q' for two regex and
then calling 'sub()' on both p and q. Can't I just do that by
employing one RegEx and then calling sub() only once?

Please enlighten me. Thanks in advance.
If you really want to replace EVERY occurrence, no regexes are needed.
I'd do something like (not tested):

fp=open(inputfilename, 'r')
contents=fp.read()
fp.close()
parts=contents.split('[[http://www.hotmail.com -Hotmail]]')
newcontents='[http://www.hotmail.com Hotmail]'.join(parts)
fp=open(outputfilename, 'w')
fp.write(newcontents)
fp.close()

-Larry
Jun 27 '08 #3
If you know that, for instance, every occurrence of '[[' needs to be
replaced with ']]', then it is much faster to use regular string
methods. If you have the text stored in the variable foo:

foo = foo.replace('[[', '[').replace(']]', ']').replace('->', '')

If you need to recognize the actual pattern, use REs:

regex = re.compile(r'\[\[(.+?) -(.+?)\]\]')
regex.sub(r'[\1 \2]', foo)
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Christopher R. Barry | last post: by
7 posts views Thread by Rob Meade | last post: by
2 posts views Thread by thehuby | last post: by
8 posts views Thread by Gordon Knote | last post: by
12 posts views Thread by Adam J. Schaff | last post: by
4 posts views Thread by wildman | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.