'Java Chess Game Regex
I am currently working on a chess game in java. I want to create a method to check the format of the string input. A typical chess move in my game is:
e2 e4
a1 a3
In general, its:
[letter][number][space][letter][number]
How can I check this format with a regex? I am fairly new to regular expression and would appreciate any help
So far, I have:
[a-h]+[1-8]+\s+[a-h]+[1-8]
Solution 1:[1]
Solution 2:[2]
Based on the general notation for chess moves, you can apply the following regex. The chess moves could look like this:
MOVE1. e4 c5
MOVE2. Sf3 Sc6
MOVE3. Lb5 g6
MOVE4. Lxc6 dxc6
MOVE5. d3 Lg7
MOVE6. h3 Sf6
MOVE7. Sc3 Sd7
MOVE8. Le3 e5
MOVE9. O-O b6
MOVE10. Sh2 Sf8
MOVE11. f4 exf4
After cutting out the first part of the line, the corresponding regex for validating whether the notation is correct looks like this:
([a-h]|\\s|[KDTLSO])([a-h]|\\d|[KDTLSx-]|\\s)([a-h]|\\s|[+#xO]|\\d)([a-h]|\\s|\\d|[+#-])(\\s|[+#O]|\\d)
Solution 3:[3]
A comprehensive regular expression to check the validity of input can be as follows:
\s*(\d{1,3})\.?\s*((?:(?:O-O(?:-O)?)|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8])|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?))\+?)(?:\s*\d+\.?\d+?m?s)?\.?\s*((?:(?:O-O(?:-O)?)|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8])|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?))\+?)?(?:\s*\d+\.?\d+?m?s)?
The explanation of the above is as follows:
\s* // Ignore whitespace before
(\d{1,3}) // Capture group 1: Move number between 1 and 999 will precede white side's move.
\.? // Literal period, in case move numbers followed by a period. The replace pattern will restore period, so it is not captured.
\s* // Ignore whitespace
( // Capture group 2: This will collect the white side's move
(?: // Start non-capturing group A: Use vertical bar | between non-capturing groups to check for castling, piece moves/captures, pawn moves/captures/promotion
(?:O-O(?:-O)?) // Non-capturing subgroup A1: For castling kingside or queenside. Change the O to 0 to work for sites that 0-0 for castling notation
|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8]) // Non-capturing subgroup A2: For piece (non-pawn) moves and piece captures
|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?) // Non-capturing subgroup A3: Pawn moves, captures, and promotions
) // End non-capturing group A
\+? // Allow plus symbol for checks (attacks on king)
) // End capturing group 2: White side's move
(?:\s*\d+\.?\d+?m?s)? // Non-capturing group B: Skip over move-times; it is possible to retain move times if you make this a capturing group
\.? // Allow period in case a time ends in a decimal point
\s* // Ignore whitespace
( // Capture group 3: This will collect the black side's move
(?: // Start non-capturing group C: Use vertical bar | between non-capturing groups to check for castling, piece moves/captures, pawn moves/captures/promotion
(?:O-O(?:-O)?) // Non-capturing subgroup C1: For castling kingside or queenside. Change the O to 0 to work for sites that 0-0 for castling notation
|(?:[KQNBR][1-8a-h]?x?[a-h]x?[1-8]) // Non-capturing subgroup C2: For piece (non-pawn) moves and piece captures
|(?:[a-h]x?[a-h]?[1-8]\=?[QRNB]?) // Non-capturing subgroup C3: Pawn moves, captures, and promotions
) // End non-capturing group C
\+? // Allow plus symbol for checks (attacks on king)
)? // End capturing group 3: Black side's move. Question mark allows final move to be white side's move without any subsequent black moves
(?:\s*\d+\.?\d+?m?s)? // Non-capturing group D: Skip over move-times; it is possible to retain move times if you make this a capturing group
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Drogo |
| Solution 3 | Mavaddat Javid |
