1 pièce(s) jointe(s)
Analyse d'un fichier PGN par les expressions régulières
Bonjour ! Je vous propose une procédure qui analyse le contenu d'un fichier PGN, représentant un ensemble de parties d'échecs.
La procédure est basée sur les expressions régulières.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
unit PGN;
interface
procedure ParsePGNText(const aText: string);
implementation
uses
RegExpr;
procedure ParsePGNText(const aText: string);
const
TAG = '\[\w+\s+"[^"]+"\]';
TAGS = '(' + TAG + '\s+)+';
FULLMOVENUMBER = '\d+\.\s+';
FULLMOVE = '([A-Za-z][\w-+#]+\s+){1,2}';
MOVE = FULLMOVENUMBER + FULLMOVE;
MOVES = '(' + MOVE + ')+';
RESULT = '(1-0|0-1|1/2-1/2|\*)';
GAME = TAGS + MOVES + RESULT;
type
TPattern = (ptGame, ptTag, ptMove, ptResult);
var
vExpr: array[TPattern] of TRegExpr;
vPattern: TPattern;
vText: string;
begin
vText := aText;
vExpr[ptGame] := TRegExpr.Create(GAME);
vExpr[ptTag] := TRegExpr.Create(TAG);
vExpr[ptMove] := TRegExpr.Create(MOVE);
vExpr[ptResult] := TRegExpr.Create(RESULT);
try
if vExpr[ptGame].Exec(vText) then
repeat
vText := vExpr[ptGame].Match[0];
with vExpr[ptTag] do
if Exec(vText) then
repeat
WriteLn('TAG ', Match[0]);
until not ExecNext;
with vExpr[ptMove] do
if Exec(vText) then
repeat
WriteLn('MOVE ', ReplaceRegExpr('\s', Match[0], ' ', FALSE));
until not ExecNext;
with vExpr[ptResult] do
if Exec(vText) then
WriteLn('RESULT ', Match[0]);
until not vExpr[ptGame].ExecNext;
finally
for vPattern in TPattern do
vExpr[vPattern].Free;
end;
end;
end. |
Qu'en pensez-vous ?