i'm PosSible
i'm PosSible

Reputation: 1393

Replace a span tag with an anchor tag using a Python regex

I have an HTML string, and I want to replace <span> tags with <a> tags.

From this:

<span>
    <span data-oe-model="demo.demo" data-oe-id="33" id="a">
        @Joseph Walters
    </span>
    hi
</span>

to this:

<span>
    <a href="demo.demo/33" id="a">@Joseph Walters</a>
    hi
</span>

I want 3 groups using RegEx: data-oe-model, data-oe-id, and name @Joseph Walters.

Note: data-oe-model and data-oe-id need to combine into href.

Is this possible with a Python RegEx?

I have tried this.

Upvotes: 2

Views: 750

Answers (2)

Headrun
Headrun

Reputation: 129

You can get the values for data-oe-model & data-oe-id first. And then use re.sub

a="""<span><span data-oe-model="demo.demo" data-oe-id="33" id="a">@Joseph Walters</span> hi</span>"""
data_oe_model = re.findall(r'data-oe-model="(.*?)"', a)[0]
data_oe_id = re.findall(r'data-oe-id="(.*?)"', a)[0]
b = re.sub('span data-oe-model="(.*?)" data-oe-id="(.*?)"', 'a href='+data_oe_model+'/'+data_oe_id, a)

print  b

Output: <span><a href=demo.demo/33 id="a">@Joseph Walters</span> hi</span>

Note: Do some validations while getting the values by list indexing.

Upvotes: 1

karthik manchala
karthik manchala

Reputation: 13640

You can use the following to match:

<span data-oe-model="([^"]*)"\s+data-oe-id="([^"]*)"\s+id="([^"]*)">(.*?)\</span>

And replace with the following:

<a href="\1\2" id="\3">\4</a>

See DEMO

Upvotes: 4

Related Questions