testString = ("<h2>Tricks</h2>"
"<a href=\"#\"><i class=\"icon-envelope\"></i></a>")
import re
re.sub("(?<=[<h2>(.+?)</h2>\s+])<a href=\"#\"><i class=\"icon-(.+?)\"></i></a>", "{{ \\1 @ \\2 }}", testString)
This produces: invalid group reference
.
Making the replacement take only \\1
, only extracts envelope
, that makes me think that the lookbehind is ignored. Is there a way to extract something from lookbehind?
I'm looking forward to produce:
<h2>Tricks</h2>
{{ Tricks @ envelope }}