今後も使えそうなのでメモ。
import re from django.core.exceptions import ObjectDoesNotExist from sample.web.models import WebPage PATTERN = r'<a href="%(dummy_url)s" >%(real_url)s</a>' HTML_TEMPLATE = re.compile(r"((http|https)://[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)", re.I) def substitute_url(m): url = m.group(0) webpage = None try: webpage = WebPage.objects.get(url=url) except ObjectDoesNotExist: webpage = WebPage() webpage.url = url webpage.save() return PATTERN % {"real_url": url, "dummy_url": "/bookmark/%s/" % (webpage.id,),} print HTML_TEMPLATE.sub(substitute_url, "http://b.hatena.ne.jp/ が面白かったので紹介します。")