Regular Expressions 101

Save & Share

  • Regex Version: ver. 5
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gxA

Test String

Code Generator

Generated Code

#include <StringConstants.au3> ; to declare the Constants of StringRegExp #include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate Local $sRegex = "(?xA)# Chess game validation regex (PCRE)" & @CRLF & _ "(?!^(?!rnbqkbnr\np{8}(?:\n-{8}){4}\nP{8}\nRNBQKBNR\n\n)) # assert initial state of board, if this is the first move" & @CRLF & _ "(?>" & @CRLF & _ " (\n)? # \1 = capture \n if black moves next (ensuring that white moves first), or unset/NPCG if white moves next" & @CRLF & _ "| # subroutines:" & @CRLF & _ " ((.|\n(?=.)){2}) # (?3) = for moving within the board, without wrapping to the next board, (?2) = (?3){2}" & @CRLF & _ " ((?= # (?4) = assert that position of just-consumed piece is vacated on the next turn" & @CRLF & _ " (\X{72}) # (?5) = skip to the position of the just-consumed piece on the next turn" & @CRLF & _ " -))" & @CRLF & _ " ((?! # (?6) = assert that the piece at the current position belongs to the current player" & @CRLF & _ " ((?=(?(1)[-A-Z]|[-a-z]))) # (?7) = assert that the piece at the current position belongs to the current player's opponent or is empty" & @CRLF & _ " ))" & @CRLF & _ " ((?5)(?(?=(.*\n)\n)[qnrb]|p)) # (?8) = black pawn that might be promoted, (?9) = .*\n" & @CRLF & _ " ( # (?10) = assert that the just-consumed piece never moved in all past turns" & @CRLF & _ " (?<=(.))" & @CRLF & _ " ((?<!(?!_|\11(?12))\PM{74}))" & @CRLF & _ " )" & @CRLF & _ " ((?5)(?(?=(?9){8}\n)[QNRB]|P)) # (?13) = white pawn that might be promoted" & @CRLF & _ ")" & @CRLF & _ "( # check if we're in check; \14 will be captured and empty if we're not in check, or unset if we are; call (?14) to assert we're not in check on the next turn" & @CRLF & _ " (?!" & @CRLF & _ " \X{0,70}" & @CRLF & _ " (?:" & @CRLF & _ " # pawns (capture diagonally)" & @CRLF & _ " (?(1)k|p)(?=(?3){7}(?2)?(?(1)P|K)) |" & @CRLF & _ " # bishops, rooks, queens, or knights" & @CRLF & _ " (?i:" & @CRLF & _ " (?<E>(?6)K)? # decide between scanning forward (<E> is unset) or backwards (<E> is captured)" & @CRLF & _ " ()? # for bishops/queens, decide between one diagonal or the other" & @CRLF & _ " (?:" & @CRLF & _ " (?(E)|((?7)[BQ])) ((?(-1)-)(?3){7}(?(-3)(?2)))+ (?(E)(?-2)) | # bishops or queens" & @CRLF & _ " (?(E)|((?7)[RQ])) (?:-*|((?(-1)-)(?3){8})+) (?(E)(?-2)) | # rooks or queens" & @CRLF & _ " (?(E)|((?7) N )) (?:(?<=..)(?2){3}|(?=.)(?2){5}|(?2){8}(?2)?) (?(E)(?-1)) # knights" & @CRLF & _ " )" & @CRLF & _ " (?(E)|(?&E)) |" & @CRLF & _ " KK|K(?3){7,9}K # kings" & @CRLF & _ " )" & @CRLF & _ " )" & @CRLF & _ " )" & @CRLF & _ ")??" & @CRLF & _ "(?>" & @CRLF & _ " (?>" & @CRLF & _ " # Handle squares that don't change (empty->empty or pieces that doesn't move)" & @CRLF & _ " (.)(?=(?5)\g{-1}) |" & @CRLF & _ " # Handle a piece that moves (and optionally captures an enemy piece)" & @CRLF & _ " (?(m)$) # allow only one move to be made per turn" & @CRLF & _ " (?>" & @CRLF & _ " (?(1)" & @CRLF & _ " (?:p(?4)(?: # black pawn" & @CRLF & _ " (?=(?3){8}((?3){9})? - (?8))(?(-1)(?=(?9){7}\n)) | # move 1 or 2 spaces forward" & @CRLF & _ " (?=(?3){7}(?2)? [A-Z](?8)) ) | # capture diagonally" & @CRLF & _ " (?=\X{73}--(?9){4}\n)(?|P(?=(p))|pP())(?<=(?=(?:-\X{8}){2}P)\PM{74}(?=\X{8}-(?5)p))\g{-1} ) # en passant" & @CRLF & _ " |" & @CRLF & _ " (?: # white pawn" & @CRLF & _ " - (?=(?13))(?=(?3){8}((?3){9})?P(?4))(?(-1)(?=(?9){4}\n)) | # move 1 or 2 spaces forward" & @CRLF & _ " [a-z](?=(?13))(?=(?3){7}(?2)? P(?4)) | # capture diagonally" & @CRLF & _ " (?=\X{73}--(?9){5}\n)(?|p(?=(P))|Pp())(?<=(?=p(?:\X{8}-){2})\PM{82}(?=-(?5)P)\PM{10})\g{-1} ) # en passant" & @CRLF & _ " ) |" & @CRLF & _ " # Castling - must be tried before rook moves unless we give up atomicity" & @CRLF & _ " # Leaves the rightmost changed square unconsumed, so that "only allow matching one of these per turn" below can't silently consume an illegal move" & @CRLF & _ " (?6)([KkRr])(?10)" & @CRLF & _ " (?!" & @CRLF & _ " (?:--)?-" & @CRLF & _ " # Verify that we aren't castling through check" & @CRLF & _ " # There's no need to check for kings, because they can't threaten check at this spot without our king already being in check" & @CRLF & _ " ()? # decide between bishops/queens or rooks/queens" & @CRLF & _ " ()? # for bishops/queens, decide between one diagonal or the other" & @CRLF & _ " (?(1)" & @CRLF & _ " (?|\X{7}(?2)?P # pawns (capture diagonally)" & @CRLF & _ " |(\X{6}|\X{10}|\X{16}(?2)?)N # knights" & @CRLF & _ " |((?(-1)-)\X{7}(?(-2)(?2)))+[BQ] # bishops or queens" & @CRLF & _ " |((?(-1)-)\X{8} )+[RQ]) # rooks or queens (only look downward, because left/right are impossible in this context)" & @CRLF & _ " | (?<=p\PM{8}|p\PM{10} # pawns (capture diagonally)" & @CRLF & _ " |(?=(?:|(?2)|\X{8}|\X{12})n)\PM{20} # knights" & @CRLF & _ " |((?<= # bishops/queens or rooks/queens" & @CRLF & _ " (?!.*\n\n)" & @CRLF & _ " (?=" & @CRLF & _ " (?(-4)" & @CRLF & _ " \X # rooks or queens (only look upward, because left/right are impossible in this context)" & @CRLF & _ " |" & @CRLF & _ " (?(-3)(?2)) # bishops or queens" & @CRLF & _ " )" & @CRLF & _ " (?:(?(-4)[rq]|[bq])|-(?-1))" & @CRLF & _ " )" & @CRLF & _ " \PM{11}" & @CRLF & _ " )))" & @CRLF & _ " )" & @CRLF & _ " )" & @CRLF & _ " (?=(?5)-(-[Kk]|[Rr])\g{-6}-)--(?=\g{-1}(?10))-?\14 |" & @CRLF & _ " # bishops, rooks, queens, knights, or kings" & @CRLF & _ " (?<e>(?7).)? # decide between scanning forward (<e> is unset) or backwards (<e> is captured)" & @CRLF & _ " ()? # for bishops/queens, decide between one diagonal or the other" & @CRLF & _ " (?=" & @CRLF & _ " (?i:" & @CRLF & _ " (?|" & @CRLF & _ " (?(e)|(B|Q)) ((?(-1)-)(?3){7}(?(-3)(?2)))+ (?(e)(B|Q)) | # bishops or queens" & @CRLF & _ " (?(e)|(R|Q)) (?:-*|((?(-1)-)(?3){8})+) (?(e)(R|Q)) | # rooks or queens" & @CRLF & _ " (?(e)|(N )) ((?<=..)(?2){3}|(?=.)(?2){5}|(?2){8}(?2)?) (?(e)(N )) | # knights" & @CRLF & _ " (?(e)|(K )) ((?3){7,9})? (?(e)(K )) # kings" & @CRLF & _ " )" & @CRLF & _ " )" & @CRLF & _ " (?(e)(?<=(?!(?7)).)(?4)|(?7).(?5)\g{-3}) # verify that the piece moved, and optionally captured piece, are of the correct color" & @CRLF & _ " )" & @CRLF & _ " (?(e)(?=(?5)\g{-1})|(?6).(?4)) # verify that the piece moved is the same type and color at its destination in the next turn's board position" & @CRLF & _ " )(?<m>) |" & @CRLF & _ " (?(+1)$)(.) # handle the destination/source square that a piece moved to/from (only allow matching one of these per turn)" & @CRLF & _ " )+\n" & @CRLF & _ ")+" & @CRLF & _ "\k<m> # assert that a move has taken place" & @CRLF & _ "(?=\n(?14)) # don't allow moving into check" & @CRLF & _ "\1? # if white moved last, black moves next, and vice-versa" Local $sString = "rnbqkbnr" & @CRLF & _ "pppppppp" & @CRLF & _ "--------" & @CRLF & _ "--------" & @CRLF & _ "--------" & @CRLF & _ "--------" & @CRLF & _ "PPPPPPPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "pppppppp" & @CRLF & _ "--------" & @CRLF & _ "--------" & @CRLF & _ "----P---" & @CRLF & _ "--------" & @CRLF & _ "PPPP-PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "pppp-ppp" & @CRLF & _ "----p---" & @CRLF & _ "--------" & @CRLF & _ "----P---" & @CRLF & _ "--------" & @CRLF & _ "PPPP-PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "pppp-ppp" & @CRLF & _ "----p---" & @CRLF & _ "--------" & @CRLF & _ "---PP---" & @CRLF & _ "--------" & @CRLF & _ "PPP--PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "ppp--ppp" & @CRLF & _ "----p---" & @CRLF & _ "---p----" & @CRLF & _ "---PP---" & @CRLF & _ "--------" & @CRLF & _ "PPP--PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "ppp--ppp" & @CRLF & _ "----p---" & @CRLF & _ "---pP---" & @CRLF & _ "---P----" & @CRLF & _ "--------" & @CRLF & _ "PPP--PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "pp---ppp" & @CRLF & _ "----p---" & @CRLF & _ "--ppP---" & @CRLF & _ "---P----" & @CRLF & _ "--------" & @CRLF & _ "PPP--PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "pp---ppp" & @CRLF & _ "----p---" & @CRLF & _ "--ppP---" & @CRLF & _ "---P----" & @CRLF & _ "--P-----" & @CRLF & _ "PP---PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "pp---ppp" & @CRLF & _ "----p---" & @CRLF & _ "---pP---" & @CRLF & _ "---p----" & @CRLF & _ "--P-----" & @CRLF & _ "PP---PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqkbnr" & @CRLF & _ "pp---ppp" & @CRLF & _ "----p---" & @CRLF & _ "---pP---" & @CRLF & _ "---P----" & @CRLF & _ "--------" & @CRLF & _ "PP---PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqk-nr" & @CRLF & _ "pp---ppp" & @CRLF & _ "----p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--------" & @CRLF & _ "PP---PPP" & @CRLF & _ "RNBQKBNR" & @CRLF & _ "" & @CRLF & _ "rnbqk-nr" & @CRLF & _ "pp---ppp" & @CRLF & _ "----p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQKBNR" & @CRLF & _ "" & @CRLF & _ "r-bqk-nr" & @CRLF & _ "pp---ppp" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQKBNR" & @CRLF & _ "" & @CRLF & _ "r-bqk-nr" & @CRLF & _ "pp---ppp" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--N--N--" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQKB-R" & @CRLF & _ "" & @CRLF & _ "r-bqk--r" & @CRLF & _ "pp--nppp" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--N--N--" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQKB-R" & @CRLF & _ "" & @CRLF & _ "r-bqk--r" & @CRLF & _ "pp--nppp" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--NB-N--" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-rk-" & @CRLF & _ "pp--nppp" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--NB-N--" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-rk-" & @CRLF & _ "pp--nppB" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--N--N--" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--nppk" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP---" & @CRLF & _ "-b-P----" & @CRLF & _ "--N--N--" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--nppk" & @CRLF & _ "--n-p---" & @CRLF & _ "---pP-N-" & @CRLF & _ "-b-P----" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--npp-" & @CRLF & _ "--n-p-k-" & @CRLF & _ "---pP-N-" & @CRLF & _ "-b-P----" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PPP" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--npp-" & @CRLF & _ "--n-p-k-" & @CRLF & _ "---pP-N-" & @CRLF & _ "-b-P---P" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--npp-" & @CRLF & _ "----p-k-" & @CRLF & _ "---pP-N-" & @CRLF & _ "-b-n---P" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-BQK--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--npp-" & @CRLF & _ "----p-k-" & @CRLF & _ "---pP-N-" & @CRLF & _ "-b-n--QP" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-B-K--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--n-p-" & @CRLF & _ "----p-k-" & @CRLF & _ "---pPpN-" & @CRLF & _ "-b-n--QP" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-B-K--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--n-p-" & @CRLF & _ "----p-k-" & @CRLF & _ "---pPpNP" & @CRLF & _ "-b-n--Q-" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-B-K--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--n-p-" & @CRLF & _ "----p--k" & @CRLF & _ "---pPpNP" & @CRLF & _ "-b-n--Q-" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-B-K--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--n-p-" & @CRLF & _ "----N--k" & @CRLF & _ "---pPp-P" & @CRLF & _ "-b-n--Q-" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-B-K--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--n---" & @CRLF & _ "----N--k" & @CRLF & _ "---pPppP" & @CRLF & _ "-b-n--Q-" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-B-K--R" & @CRLF & _ "" & @CRLF & _ "r-bq-r--" & @CRLF & _ "pp--n---" & @CRLF & _ "----N-Pk" & @CRLF & _ "---pPp--" & @CRLF & _ "-b-n--Q-" & @CRLF & _ "--N-----" & @CRLF & _ "PP---PP-" & @CRLF & _ "R-B-K--R" & @CRLF & _ "" & @CRLF & _ "WIN BY CHECKMATE" Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYGLOBALFULLMATCH) Local $aFullArray[0] For $i = 0 To UBound($aArray) -1 _ArrayConcatenate($aFullArray, $aArray[$i]) Next $aArray = $aFullArray ; Present the entire match result _ArrayDisplay($aArray, "Result")

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm