Reputation: 1393
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
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
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