import re
regex = re.compile(r"(\d{1,2})\s*-\s*(\d{1,2})", flags=re.MULTILINE)
test_str = ("PERU\n"
"Liga 2\n"
"]\n"
"Final\n"
"Santa Rosa\n\n"
"0\n"
" - \n"
"3\n\n"
"Molinos El Pirata\n\n"
"73 \n"
"Chavelines\n\n"
"1\n"
" - \n"
"0\n\n"
"Deportivo Coopsol\n\n"
"20:30\n"
"Comerciantes Unidos\n\n"
"-\n\n"
"Juan Aurich\n"
"22:45\n"
"Santos FC\n\n"
"-\n\n"
"Huaral\n\n"
"90+1 \n"
"Pogon Szczecin\n\n"
"2\n"
" - \n"
"0\n\n"
"Stal Mielec\n\n"
"live\n"
"20:30\n"
"Plock\n\n"
"-\n\n"
"Gornik Z.\n\n"
"21:15\n"
"Farense\n\n"
"-\n\n"
"Maritimo\n")
subst = "$1-$2"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html