Sebastian Smolorz Sebastian Smolorz - 1 year ago 215
Python Question

Regex search For HTML Tag with UUID

I'm trying to match a single HTML tag with an

attribute which is a UUID. I tested it with an external resource to make sure the regex is correct with the same input string. The UUID is extracted dynamically so the string replacement is necessary.

The output I would would expect is for the last line to print:

<tr class="ref_row" id="b9060ff1-015d-4089-a193-8fef57e7c2ef">

This is the code I tried:

content = '<tbody><tr class="ref_row" id="b9060ff1-015d-4089-a193-8fef57e7c2ef"><td><b>01/08/2016 14:41:00</b></td>'
ref = 'b9060ff1-015d-4089-a193-8fef57e7c2ef'
regex = '<[^>]+?id=\"%s\"[^<]*?>' % ref
element_to_link =, content)
print element_to_link.string

The output I get when printing is the whole input string, which would suggest the regex is incorrect. What's going on here?

Please don't suggest that I use Beautiful Soup, this should be possible with regular expressions.

Answer Source

Why won't you use group method? This works for me:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download