import res = '''02/58 9802/58 /9802/58 951802/58 /98''' print(re.sub(r'(\d{1,2}/\d{1,2}) /?(?:(\d{4}|\d{2}))', r'\g<1>/\g<2>', s))
Standard input is empty
02/58/98 02/58/98 02/58/9518 02/58/98
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!