search/replace in Python

Oliver Andrich oliver.andrich at gmail.com
Sat May 28 02:54:20 EDT 2005


Hi,

2005/5/28, Vamsee Krishna Gomatam <vamsee_k at nospam.students.iiit.ac.in>:
> Hello,
>         I'm having some problems understanding Regexps in Python. I want
> to replace "<google>PHRASE</google>" with
> "<a href=http://www.google.com/search?q=PHRASE>PHRASE</a>" in a block of
> text. How can I achieve this in Python? Sorry for the naive question but
> the documentation is really bad :-(

it is pretty easy and straightforward. After you imported the re
module, you can do the job with re.sub or if you have to do it often,
then you can compile the regex first with re.compile
and afterwards use the sub method of the compiled regex.

The interesting part is

re.sub(r"<google>(.*)</google>",r"<a
href=http://www.google.com/search?q=\1>\1</a>", text)

cause here the job is done. The first raw string is the regex pattern.
The second one is the target where \1 is replaced by everything
enclosed in () in the first regex.

Here is the output of my ipython session.

In [15]: text="This is a <google>Python</google>. And some random
nonsense test....."

In [16]: re.sub(r"<google>(.*)</google>",r"<a
href=http://www.google.com/search?q=\1>\1</a>", text)

Out[16]: 'This is a <a
href=http://www.google.com/search?q=Python>Python</a>. And some random
nonsense test.....'

Best regards,
Oliver



More information about the Python-list mailing list