$re = '/((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})[^\)\(])+)|((?:\)\s\())/';
$str = '1. e4 e5 2. Nf3 Nc6 3. c3 d5 4. Bd3?! f5 5. exd5?! Qxd5 6. Qe2 Be6 7. Bc2 O-O-O 8. O-O Nf6 9. d3 h6 10. Bb3 Qd6 11. Rd1? Bxb3 12. axb3 e4 13. Ne1 Ng4 14. g3 g5 15. Be3 h5!? 16. Bxg5 h4! 17. Bf4? ( After 17. Bxd8 ( 17. h3 hxg3! 18. hxg4 Qg6! 19. Bxd8 Rh1+! ) 17... hxg3 18. hxg3 e3! 19. f4 Kxd8! 20. Nf3 Qh6 21. Qg2 e2 22. Re1 Ne3 23. Qh2 Qg7! Black was only better. Now comes a gorgeous queen sacrifice. ) 17... hxg3!! 18. Bxd6 gxf2+ 19. Qxf2?! Richard correctly evaluates the exposed nature of his king and immediately returns the queen. Why"?!" to 19.Qxf2 then? Simply because now, with material about equal, white\'s position is absolutely hopeless anyway. The king hunt is relentless. There are several beautiful lines here. ( 19. Kf1 Bxd6 20. dxe4 Nxh2+! 21. Kxf2 Bc5+ 22. Kg3 f4+ 23. Kxf4 Rdf8+ 24. Kg3 Nf1+ 25. Kg4 Ne5+ 26. Kg5 Be7 checkmate ) ( Or 19. Kg2 Bxd6 20. Nf3!? exf3+ 21. Qxf3 f1=Q+! 22. Rxf1 Rxh2+ 23. Kg1 Bc5+ 24. d4 Nxd4! 25. cxd4 Bxd4+ 26. Rf2 Bxf2+ 27. Kf1 Ne3+! 28. Ke2 Bh4+ 29. Kxe3 Bg5+ winning everything. ) ( But the most astonishing was: 19. Kg2 Bxd6 20. h3 Rxh3!! 21. Kxh3 Rh8+ 22. Kg2 Rh2+ 23. Kf1 Rh1+ 24. Kg2 Rg1+ 25. Kh3 Ne3! 26. Kh4 Be7+ 27. Kh5 Rh1+ 28. Kg6 Ne5+ 29. Kg7 Nd5! 30. dxe4 Bf6+ 31. Kg8 Rh8 checkmate. Of course in sacrificing the queen I didn\'t see all the lines so deeply. I only clearly concluded that I\'ve got a terrific attack on his king. In situations like this you must trust your intuition. Let your opponent find the best defense and adjust your calculations for accuracy after each of his moves. ) 19... Nxf2 20. Kxf2 Bxd6 Richard correctly judges the vulnerability of his king and immediately returns the queen. 21. dxe4 fxe4!? 22. Na3 Rxh2+ 23. Ke3 Bc5+ 24. Kxe4 Re2+ 25. Kf3 Re3+ 26. Kg2 Rg8+ 27. Kf1 Rf8+ 28. Kg2 Re7 29. Nf3 Rg7+ 30. Kh3 Rh8+ 31. Nh4 Bf2 0-1';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php