我能够从pgn游戏中获取TAG,但是当我想要否定它以便获得移动时,它似乎不起作用。
模式只获得TAG(工作精细)
(\[[^\\[]*\])
获取除TAGs模式以外的所有内容的模式(FAIL)
^(\[[^\\[]*\]).*$
用于测试的PGN游戏
[Event "Wch U16"]
[Site "Wattignies"]
[Date "1976.??.??"]
[Round "?"]
[White "Dunne, David Joseph"]
[Black "Kasparov, Gary"]
[Result "0-1"]
[WhiteElo ""]
[BlackElo ""]
[ECO "B51"]
1. e2e4 c7c5 2. g1f3 d7d6 3. f1b5+ b8d7 4. d2d4 g8f6 5. b1c3 c5d4 6. d1d4
e7e5 7. d4d3 h7h6 8. c1e3 f8e7 9. e1g1 e8g8 10. a1d1 a7a6 11. b5c4 d8c7 12.
a2a4 d7c5 13. e3c5 c7c5 14. c3d5 f6d5 15. c4d5 a8b8 16. f3d2 c8d7 17. d5b3
b7b5 18. a4a5 b8c8 19. d2f3 d7e6 20. f1e1 f8d8 21. e1e2 c8c7 22. e2d2 d8c8
23. h2h3 c5b4 24. b3e6 f7e6 25. d3b3 c7c4 26. d2e2 b4b3 27. c2b3 c4c1 28.
d1c1 c8c1+ 29. g1h2 g8f7 30. f3e1 e7d8 31. e1d3 c1d1 32. d3b4 d8a5 33. b4a6
d1c1 34. b3b4 a5b6 35. b2b3 c1c3 36. e2a2 c3b3 37. a2c2 b6d4 38. c2c7+ f7f6
39. f2f3 b3a3 40. c7c6 a3c3 41. c6c7 c3c7 42. a6c7 d4c3 43. c7b5 c3b4 44.
b5a7 f6f7 45. h2g3 f7e8 46. g3g4 e8d7 47. f3f4 g7g6 48. h3h4 d7c7 49. f4f5
e6f5+ 50. e4f5 g6f5+ 51. g4f5 c7b7 52. a7b5 b7b6 53. b5d6 b4d6 54. g2g4
b6c6 55. g4g5 h6g5 56. h4g5 c6d5 57. g5g6 d6f8 0-1
编辑#1 预期输出将是(匹配部分)
1. e2e4 c7c5 2. g1f3 d7d6 3. f1b5+ b8d7 4. d2d4 g8f6 5. b1c3 c5d4 6. d1d4
e7e5 7. d4d3 h7h6 8. c1e3 f8e7 9. e1g1 e8g8 10. a1d1 a7a6 11. b5c4 d8c7 12.
a2a4 d7c5 13. e3c5 c7c5 14. c3d5 f6d5 15. c4d5 a8b8 16. f3d2 c8d7 17. d5b3
b7b5 18. a4a5 b8c8 19. d2f3 d7e6 20. f1e1 f8d8 21. e1e2 c8c7 22. e2d2 d8c8
23. h2h3 c5b4 24. b3e6 f7e6 25. d3b3 c7c4 26. d2e2 b4b3 27. c2b3 c4c1 28.
d1c1 c8c1+ 29. g1h2 g8f7 30. f3e1 e7d8 31. e1d3 c1d1 32. d3b4 d8a5 33. b4a6
d1c1 34. b3b4 a5b6 35. b2b3 c1c3 36. e2a2 c3b3 37. a2c2 b6d4 38. c2c7+ f7f6
39. f2f3 b3a3 40. c7c6 a3c3 41. c6c7 c3c7 42. a6c7 d4c3 43. c7b5 c3b4 44.
b5a7 f6f7 45. h2g3 f7e8 46. g3g4 e8d7 47. f3f4 g7g6 48. h3h4 d7c7 49. f4f5
e6f5+ 50. e4f5 g6f5+ 51. g4f5 c7b7 52. a7b5 b7b6 53. b5d6 b4d6 54. g2g4
b6c6 55. g4g5 h6g5 56. h4g5 c6d5 57. g5g6 d6f8 0-1
编辑#2使用@revo方法我能够使其在C#中工作
String MOVES_PATTERN = @"(?m)^\s*[^][]+";
//Test moves
this.tb_uci_output.Text += getMoves(content, MOVES_PATTERN);
private String getMoves(String sentence, String pattern)
{
String moves = "";
MatchCollection m = Regex.Matches(sentence, pattern);
if (m.Count > 0)
{
foreach (Match match in m)
{
//Console.WriteLine(match.Value);
moves += match.Value + Environment.NewLine;
}
//Console.WriteLine("length of match " + m.Count);
}
else
{
Console.WriteLine("error while parsing Regular expression");
}
return moves;
}
我会提前感谢任何输入:)