Consider the following string (edit: this is not a parsing HTML with regexs questions. Rather just an exercise with named groups):
s = """<T1>
<A1>
lorem ipsum
</A1>
</T1>"""
Is it possible to use re.sub
and named groups to transform the string into this result?
<T1>
<test number="1">
lorem ipsum
</test>
</T1>
Right now I have the following code:
import re
regex = re.compile("(<(?P<end>\/*)A(?P<number>\d+)>)")
print regex.sub('<\g<end>test number="\g<number>">', s)
which gives the following result
<T1>
<test number="1">
lorem ipsum
</test number="1">
</T1>
Can an |
operator be used like in this question?