regex - Python regular expression replace string -
i have following code:
a="32<2>fdssa</2>ffdsa32" re.sub(r'<(\d+)>|</(\d+)>',"item",a)
the result get:
32itemfdssaitemffdsa32
i want result:
32<item>fdssa</item>ffdsa32
you need capture </
part.
re.sub(r'(</?)\d+>',r"\1item>",a)
since made /
optional, (</?)
capture <
or </
example:
>>> a="32<2>fdssa</2>ffdsa32" >>> re.sub(r'(</?)\d+>',r"\1item>",a) '32<item>fdssa</item>ffdsa32'
Comments
Post a Comment