Hi, 2005/5/28, Vamsee Krishna Gomatam <[EMAIL PROTECTED]>: > 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 -- http://mail.python.org/mailman/listinfo/python-list