import re

# <a> followed by another <a> without any intervening </a>s
link_inside_link_regex = re.compile(
    ur"(?P<outer_link><a ((?!</a>).)*)(<a ((?!</a>).)*>"
    ur"(?P<internal_content>((?!</a>).)*)</a>)",
    re.IGNORECASE | re.DOTALL)


def flatten_links(text):
    """
    Fix <a> elements that have embedded <a> elements by
    replacing the internal <a> element with its content.
    Assumes that the text does not span multiple lines and that
    the <a> tags are lowercase.
    """

    while True:
        text, sub_count = link_inside_link_regex.subn(
            ur"\g<outer_link>\g<internal_content>", text)
        if sub_count == 0:
            return text # Return only when no more subs possible

@@ -64,16 +64,118 @@
 g %3C/a%3Es%0A
+# outer_link - partial outer element up to the inner link%0A# inner_content - content of the inner_link%0A
 link_ins
@@ -245,46 +245,24 @@
 .)*)
-(
 %3Ca 
-((?!%3C/a%3E).)*%3E%22%0A    ur%22
+.*?%3E
 (?P%3Cin
-t
+n
 er
-nal
 _con
@@ -270,26 +270,16 @@
 ent%3E
-((?!%3C/a%3E).)*
+.*?
 )%3C/a%3E
-)
 %22,%0A 
@@ -460,104 +460,8 @@
 nt.%0A
-    Assumes that the text does not span multiple lines and that%0A    the %3Ca%3E tags are lowercase.%0A
     
@@ -570,22 +570,19 @@
 nk%3E%5Cg%3Cin
-t
+n
 er
-nal
 _content
@@ -645,46 +645,5 @@
 text
- # Return only when no more subs possible
 %0A

Simplify regex using non-greedy qualifier