0

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]
rutger1490
  • 19
  • 2
  • 1
    Does this answer your question? [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – sleepToken Mar 04 '20 at 19:13
  • @CarySwoveland I can understand this. Unfortunately, I could not see any attempts of OP though I understand that OP is new at regex and OP just showed us the attempts so now I am going to open the post :) – Pavneet_Singh Mar 04 '20 at 19:29
  • My compiler states that i am not using a valid escape sequence? @Pavneet_Singh – rutger1490 Mar 04 '20 at 19:33
  • use double `\\s` to escape the `\s` and remove `+` – Pavneet_Singh Mar 04 '20 at 19:34
  • I'm going to add a warning (because I've fallen into similar, _terrible_ ideas in the past once I've gotten regex involved in a process). Finding the moves like this is fine... Do _not_ start trying to verify if a move is legal with regex. – BeUndead Mar 04 '20 at 20:20
  • 1
    @BeUndead I have seperate logic that determines whether a move is valid. I just needed to do some initial input verification to make sure the input is properly formatted. – rutger1490 Mar 04 '20 at 23:23
  • @rutger1490 - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 22 '20 at 10:59

3 Answers3

2

([a-h][1-8] [a-h][1-8])

https://regexr.com/ is a great resource that you should check.

Huatxu
  • 111
  • 5
0

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)
Drogo
  • 3
  • 1
-1

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
Mavaddat Javid
  • 491
  • 4
  • 19
  • I'm not sure what the rationale would be for trying to capture so much specific "chess move logic" in the validation regex. You just end up with a difficult-to-verify expression whose logic will need to be duplicated in the game implementation in any case? – Neil Coffey Jul 08 '21 at 21:46