Regex to match something based on what matched

I am trying to write a regular expression to check moves in algebraic chess notation . Here is what I still have:

/
    O-O(-O)?                 # Castling
    |[KQRBN]x?[a-h][1-8]\+?  # Most normal moves and captures
/

If I am lost, advance the pawn.

A knight, bishop, or center pawn can only advance on the file from which it is launched, or on both sides of the file through capture. The rook pawn can advance by moving straight or capturing in one direction, depending on whether it is on a- or h files. So something like

/[a-h](x[a-h])?[18]=[QRBN]\+?/

does not work because fxh8 is not a valid move (only fxe8 and fxg8). I could go a long route with

/(a(xb)?|h(xg)?|b(x[ac])?.../ # insert 5 more files in place of the ...

. , - . - :

/([b-g])(x(\1±1))?/

, b , .

+4
1

, , .

- - .

- .

+1

Source: https://habr.com/ru/post/1545501/


All Articles