search/replace in Python
Leif K-Brooks
eurleif at ecritters.biz
Sat May 28 06:44:08 EDT 2005
Oliver Andrich wrote:
> re.sub(r"<google>(.*)</google>",r"<a
> href=http://www.google.com/search?q=\1>\1</a>", text)
For real-world use you'll want to URL encode and entityify the text:
import cgi
import urllib
def google_link(text):
text = text.group(1)
return '<a href="%s">%s</a>' % (cgi.escape(urllib.quote(text)),
cgi.escape(text))
re.sub(r"<google>(.*)</google>", google_link, "<google>foo bar</google>)
More information about the Python-list
mailing list