How do I add a tag for numbers that are enclosed in brackets using python regex?

Default strings:

strings123[abc123def456]strings456

Add tag for number:

strings[abc<span>123</span>def<span>456</span>]strings

-2


source to share


1 answer


Search by:

(\d+)

      

and replace with:

<span>\1</span>

      



Demo Regex

Sample source:

import re
regex = r"(\d+)"
test_str = "strings[abc123def456]strings"
subst = "<span>\\1</span>"
result = re.sub(regex, subst, test_str, 0)
if result:
    print (result)

      

+2


source







All Articles