我正在尝试解析国际象棋PGN文件,但是没有设法获取多个动作和标签的所有实例。
我已经this regex了,并添加了两个捕获组名称:tag和move,这引导我进入下面的一个:
private val pgnRegex = """(?<pgnGame>\s*(?<tag>\[\s*(?<tagName>\w+)\s*"(?<tagValue>[^"]*)"\s*\]\s*)*(?<move>(?<moveNumber>\d+)(?<moveMarker>\.|\.{3})\s*(?<moveValue>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?)(?:\s*(?<moveValue2>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?))?\s*(?:\(\s*(?<variation>(?:(?<varMoveNumber>\d+)(?<varMoveMarker>\.|\.{3})\s*(?<varMoveValue>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?)(?:\s*(?<varMoveValue2>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?))?\s*(?:\((?<varVariation>.*)\)\s*)?(?:\{(?<varComment>[^\}]*?)\}\s*)?)*)\s*\)\s*)*(?:\{(?<comment>[^\}]*?)\}\s*)?)*(?<endMarker>1\-?0|0\-?1|1/2\-?1/2|\*)?\s*)""".r
我试图使用以下对象解析Pgn:
package com.loloof64.scala.pgn_to_pictures.pgn_parsing
import java.io.File
import scala.io.Source
object PgnReader {
def parseFile(inputFile:File) = {
val pgnStream = Source.fromFile(inputFile)
val pgnStr = pgnStream.mkString
pgnRegex.findAllIn(pgnStr).matchData.foreach{
currMatchData => if (!currMatchData.toString.isEmpty) {
currMatchData.subgroups.foreach { currGroup => println(s"Group id => ${currGroup}") }
}
}
pgnStream.close
}
/* thanks to Siderite : http://regexlib.com/REDetails.aspx?regexp_id=3355
* Adapted by Laurent Bernabe : added some capturing groups (tag and move) */
private val pgnRegex = """(?<pgnGame>\s*(?<tag>\[\s*(?<tagName>\w+)\s*"(?<tagValue>[^"]*)"\s*\]\s*)*(?<move>(?<moveNumber>\d+)(?<moveMarker>\.|\.{3})\s*(?<moveValue>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?)(?:\s*(?<moveValue2>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?))?\s*(?:\(\s*(?<variation>(?:(?<varMoveNumber>\d+)(?<varMoveMarker>\.|\.{3})\s*(?<varMoveValue>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?)(?:\s*(?<varMoveValue2>(?:[PNBRQK]?[a-h]?[1-8]?x?(?:[a-h][1-8]|[NBRQK])(?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?))?\s*(?:\((?<varVariation>.*)\)\s*)?(?:\{(?<varComment>[^\}]*?)\}\s*)?)*)\s*\)\s*)*(?:\{(?<comment>[^\}]*?)\}\s*)?)*(?<endMarker>1\-?0|0\-?1|1/2\-?1/2|\*)?\s*)""".r
}
以下PGN:
[Event "Live Chess"]
[Site "Chess.com"]
[Date "2014.11.17"]
[White "Rafael1978"]
[Black "AChessGeek"]
[Result "1/2-1/2"]
[WhiteElo "1332"]
[BlackElo "1350"]
[TimeControl "15|10"]
[Termination "Partie nulle par pat"]
1.e4 c6 2.Nf3 d5 3.exd5 cxd5 4.d4 Nf6 5.Bg5 e6 6.Ne5 Be7 7.Bxf6 Bxf6 8.Qh5 Bxe5 9.Q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/2
Group id => [Termination "Partie nulle par pat"]
Group id => Termination
Group id => Partie nulle par pat
Group id => 85.Kh6
Group id => 85
Group id => .
Group id => Kh6
Group id => -O
Group id => null
Group id => Kh8
Group id => -O
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => null
Group id => 1/2-1/2
如何实现以下结果(仅针对主线,忽略目前可能发生的所有变化)?
Tag => [Site "Chess.com"]
Tag => [Date "2014.11.17"]
Tag => [White "Rafael1978"]
Tag => [Black "AChessGeek"]
Tag => [Result "1/2-1/2"]
Tag => [WhiteElo "1332"]
Tag => [BlackElo "1350"]
Tag => [TimeControl "15|10"]
Tag => [Termination "Partie nulle par pat"]
Move => e4
Move => c6
...
Move => Kh6