text = """ <!1!>safafdsf</!1!> <!2!>safafdsf</!2!> """ reg = re.compile(r'(!\d+!)>') xx = reg.sub('li>', text)