Regular Expressions 101

Community Patterns

FEN Regex

2

Regular Expression
PCRE (PHP <7.3)

/
^ (?# Piece Placement Section)(?<PiecePlacement>((?<RankItem>[pnbrqkPNBRQK1-8]{1,8})\/?){8}) (?# Section Separator)\s+ (?# Side To Move Section) (?<SideToMove>b|w) (?# Section Separator)\s+ (?# Castling Ability) (?<Castling>-|K?Q?k?q) (?# Section Separator)\s+ (?# En passant) (?<EnPassant>-|[a-h][3-6]) (?# Section Separator)\s+ (?# Half Move Clock) (?<HalfMoveClock>\d+) (?# Section Separator)\s+ (?# Full Move Number) (?<FullMoveNumber>\d+) \s* $
/
x

Description

This will match an FEN string (https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation). It is not strict in having each rank separated by a '' character, although it is strict in the order of characters for the Castling section. Anything not following the order of KQkq in the case where castling is available, will fail the Regex. It is also case sensitive for the Side and the "en passant" column letter.

Submitted by NazarioJL - 8 years ago