import retext = "§s 23, 12"print(re.sub(r'§\w*\s+\d+(?:,\s*\d+)*', lambda x: re.sub(r'\s+', '_', x.group()), text))
Standard input is empty
§s_23,_12
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!