import re
regex = re.compile(r"\s*(\d{1,3})\.?\s*((?:(?:O-O(?:-O)?)|(?:[KQNBR][a-h]?x?[a-h]x?[1-8])|(?:[a-h]x?[a-h]?[1-8]))\+?\#?)(?:\s*\d+\.?\d+?m?s?)?\s*((?:(?:O-O(?:-O)?)|(?:[KQNBR][a-h]?x?[a-h]x?[1-8])|(?:[a-h]x?[a-h]?[1-8]))\+?\#?)(?:\s*\d+\.?\d+?m?s?)?")
test_str = ("[Event \"?\"] [Site \"?\"] [Date \"2018.04.11\"] [Round \"?\"] [White \"Bond\"] [Black \"Tor\"] [Result \"1/2-1/2\"] [ECO \"C88\"] [Annotator \"Top\"] [PlyCount \"26\"] [SourceDate \"2018.04.11\"] [SourceVersionDate \"2018.04.11\"]\n"
"1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 O-O 8. h3 Bb7 9. d3 d6 10. a3 Nb8 11. Nbd2 Nbd7 12. Nf1 Re8 13. Ng3 Bf8 1/2-1/2")
subst = "$1.$2.$3\\n"
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