Adding anchors to h2 in text using python and regexp

Posted by Oli on Stack Overflow See other posts from Stack Overflow or by Oli
Published on 2010-05-25T15:18:43Z Indexed on 2010/05/25 15:21 UTC
Read the original article Hit count: 143

Filed under:
|

I'm trying to add anchors to all h2's in my html, using python. This code will add those anchors, but I need to fill the name of the anchors too.

Any idea if the name can be the number of the match in the loop or a slugified version of the text between the h2 tags?

Here's the code so far:

regex = '(?P<name><h2>.*?</h2>)'
text = re.sub(regex, "<a name=''/>"+r"\g<name>", text)

© Stack Overflow or respective owner

Related posts about python

Related posts about html