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

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -