Regular Expressions 101

Save & Share

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

/
/
gm

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 = "(?m)(\/\/.*+|\/[*]{1,2}([^*]*[^*\/]|\R++|[*]+[^*\/])*[*]\/)" Local $sString = "// This file is part of www.nand2tetris.org" & @CRLF & _ "// and the book "The Elements of Computing Systems"" & @CRLF & _ "// by Nisan and Schocken, MIT Press." & @CRLF & _ "// File name: projects/10/ArrayTest/Main.jack" & @CRLF & _ "" & @CRLF & _ "// (identical to projects/09/Average/Main.jack)" & @CRLF & _ "" & @CRLF & _ "/** Computes the average of a sequence of integers. */" & @CRLF & _ "class Main {" & @CRLF & _ " function void main() {" & @CRLF & _ " var Array a;" & @CRLF & _ " var int length;" & @CRLF & _ " var int i, sum;" & @CRLF & _ " " & @CRLF & _ " let length = Keyboard.readInt("HOW MANY NUMBERS? ");" & @CRLF & _ " let a = Array.new(length);" & @CRLF & _ " let i = 0;" & @CRLF & _ " " & @CRLF & _ " while (i < length) {" & @CRLF & _ " let a[i] = Keyboard.readInt("ENTER THE NEXT NUMBER: ");" & @CRLF & _ " let i = i + 1;" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " let i = 0;" & @CRLF & _ " let sum = 0;" & @CRLF & _ " " & @CRLF & _ " while (i < length) {" & @CRLF & _ " let sum = sum + a[i];" & @CRLF & _ " let i = i + 1;" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " do Output.printString("THE AVERAGE IS: ");" & @CRLF & _ " do Output.printInt(sum / length);" & @CRLF & _ " do Output.println();" & @CRLF & _ " " & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "// This file is part of www.nand2tetris.org" & @CRLF & _ "// and the book "The Elements of Computing Systems"" & @CRLF & _ "// by Nisan and Schocken, MIT Press." & @CRLF & _ "// File name: projects/10/ExpressionLessSquare/Main.jack" & @CRLF & _ "" & @CRLF & _ "/** Expressionless version of projects/10/Square/Main.jack. */" & @CRLF & _ "" & @CRLF & _ "class Main {" & @CRLF & _ " static boolean test; // Added for testing -- there is no static keyword" & @CRLF & _ " // in the Square files." & @CRLF & _ "" & @CRLF & _ " function void main() {" & @CRLF & _ " var SquareGame game;" & @CRLF & _ " let game = game;" & @CRLF & _ " do game.run();" & @CRLF & _ " do game.dispose();" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " function void more() { // Added to test Jack syntax that is not used in" & @CRLF & _ " var boolean b; // the Square files." & @CRLF & _ " if (b) {" & @CRLF & _ " }" & @CRLF & _ " else { // There is no else keyword in the Square files." & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "// This file is part of www.nand2tetris.org" & @CRLF & _ "// and the book "The Elements of Computing Systems"" & @CRLF & _ "// by Nisan and Schocken, MIT Press." & @CRLF & _ "/// File name: projects/10/ExpressionLessSquare/Square.jack" & @CRLF & _ "" & @CRLF & _ "/** Expressionless version of projects/10/Square/Square.jack. */" & @CRLF & _ "" & @CRLF & _ "class Square {" & @CRLF & _ "" & @CRLF & _ " field int x, y; " & @CRLF & _ " field int size; " & @CRLF & _ "" & @CRLF & _ " constructor Square new(int Ax, int Ay, int Asize) {" & @CRLF & _ " let x = Ax;" & @CRLF & _ " let y = Ay;" & @CRLF & _ " let size = Asize;" & @CRLF & _ " do draw();" & @CRLF & _ " return x;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void dispose() {" & @CRLF & _ " do Memory.deAlloc(this);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void draw() {" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void erase() {" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void incSize() {" & @CRLF & _ " if (x) {" & @CRLF & _ " do erase();" & @CRLF & _ " let size = size;" & @CRLF & _ " do draw();" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void decSize() {" & @CRLF & _ " if (size) {" & @CRLF & _ " do erase();" & @CRLF & _ " let size = size;" & @CRLF & _ " do draw();" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void moveUp() {" & @CRLF & _ " if (y) {" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " let y = y;" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void moveDown() {" & @CRLF & _ " if (y) {" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " let y = y;" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void moveLeft() {" & @CRLF & _ " if (x) {" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " let x = x;" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void moveRight() {" & @CRLF & _ " if (x) {" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " let x = x;" & @CRLF & _ " do Screen.setColor(x);" & @CRLF & _ " do Screen.drawRectangle(x, y, x, y);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "} " & @CRLF & _ "// This file is part of www.nand2tetris.org" & @CRLF & _ "// and the book "The Elements of Computing Systems"" & @CRLF & _ "// by Nisan and Schocken, MIT Press." & @CRLF & _ "// File name: projects/10/ExpressionLessSquare/SquareGame.jack" & @CRLF & _ "" & @CRLF & _ "/** Expressionless version of projects/10/Square/SquareGame.jack. */" & @CRLF & _ "" & @CRLF & _ "class SquareGame {" & @CRLF & _ " field Square square; " & @CRLF & _ " field int direction; " & @CRLF & _ "" & @CRLF & _ " constructor SquareGame new() {" & @CRLF & _ " let square = square;" & @CRLF & _ " let direction = direction;" & @CRLF & _ " return square;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void dispose() {" & @CRLF & _ " do square.dispose();" & @CRLF & _ " do Memory.deAlloc(square);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void moveSquare() {" & @CRLF & _ " if (direction) { do square.moveUp(); }" & @CRLF & _ " if (direction) { do square.moveDown(); }" & @CRLF & _ " if (direction) { do square.moveLeft(); }" & @CRLF & _ " if (direction) { do square.moveRight(); }" & @CRLF & _ " do Sys.wait(direction);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " method void run() {" & @CRLF & _ " var char key;" & @CRLF & _ " var boolean exit;" & @CRLF & _ " " & @CRLF & _ " let exit = key;" & @CRLF & _ " while (exit) {" & @CRLF & _ " while (key) {" & @CRLF & _ " let key = key;" & @CRLF & _ " do moveSquare();" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " if (key) { let exit = exit; }" & @CRLF & _ " if (key) { do square.decSize(); }" & @CRLF & _ " if (key) { do square.incSize(); }" & @CRLF & _ " if (key) { let direction = exit; }" & @CRLF & _ " if (key) { let direction = key; }" & @CRLF & _ " if (key) { let direction = square; }" & @CRLF & _ " if (key) { let direction = direction; }" & @CRLF & _ "" & @CRLF & _ " while (key) {" & @CRLF & _ " let key = key;" & @CRLF & _ " do moveSquare();" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "// This file is part of www.nand2tetris.org" & @CRLF & _ "// and the book "The Elements of Computing Systems"" & @CRLF & _ "// by Nisan and Schocken, MIT Press." & @CRLF & _ "// File name: projects/10/Square/Main.jack" & @CRLF & _ "" & @CRLF & _ "// (derived from projects/09/Square/Main.jack, with testing additions)" & @CRLF & _ "" & @CRLF & _ "/** Initializes a new Square Dance game and starts running it. */" & @CRLF & _ "class Main {" & @CRLF & _ " static boolean test; // Added for testing -- there is no static keyword" & @CRLF & _ " // in the Square files." & @CRLF & _ " function void main() {" & @CRLF & _ " var SquareGame game;" & @CRLF & _ " let game = SquareGame.new();" & @CRLF & _ " do game.run();" & @CRLF & _ " do game.dispose();" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " function void more() { // Added to test Jack syntax that is not used in" & @CRLF & _ " var int i, j; // the Square files." & @CRLF & _ " var String s;" & @CRLF & _ " var Array a;" & @CRLF & _ " if (false) {" & @CRLF & _ " let s = "string constant";" & @CRLF & _ " let s = null;" & @CRLF & _ " let a[1] = a[2];" & @CRLF & _ " }" & @CRLF & _ " else { // There is no else keyword in the Square files." & @CRLF & _ " let i = i * (-j);" & @CRLF & _ " let j = j / (-2); // note: unary negate constant 2" & @CRLF & _ " let i = i | j;" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "// This file is part of www.nand2tetris.org" & @CRLF & _ "// and the book "The Elements of Computing Systems"" & @CRLF & _ "// by Nisan and Schocken, MIT Press." & @CRLF & _ "// File name: projects/10/Square/Square.jack" & @CRLF & _ "" & @CRLF & _ "// (same as projects/09/Square/Square.jack)" & @CRLF & _ "" & @CRLF & _ "/** Implements a graphical square. */" & @CRLF & _ "class Square {" & @CRLF & _ "" & @CRLF & _ " field int x, y; // screen location of the square's top-left corner" & @CRLF & _ " field int size; // length of this square, in pixels" & @CRLF & _ "" & @CRLF & _ " /** Constructs a new square with a given location and size. */" & @CRLF & _ " constructor Square new(int Ax, int Ay, int Asize) {" & @CRLF & _ " let x = Ax;" & @CRLF & _ " let y = Ay;" & @CRLF & _ " let size = Asize;" & @CRLF & _ " do draw();" & @CRLF & _ " return this;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Disposes this square. */" & @CRLF & _ " method void dispose() {" & @CRLF & _ " do Memory.deAlloc(this);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Draws the square on the screen. */" & @CRLF & _ " method void draw() {" & @CRLF & _ " do Screen.setColor(true);" & @CRLF & _ " do Screen.drawRectangle(x, y, x + size, y + size);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Erases the square from the screen. */" & @CRLF & _ " method void erase() {" & @CRLF & _ " do Screen.setColor(false);" & @CRLF & _ " do Screen.drawRectangle(x, y, x + size, y + size);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Increments the square size by 2 pixels. */" & @CRLF & _ " method void incSize() {" & @CRLF & _ " if (((y + size) < 254) & ((x + size) < 510)) {" & @CRLF & _ " do erase();" & @CRLF & _ " let size = size + 2;" & @CRLF & _ " do draw();" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Decrements the square size by 2 pixels. */" & @CRLF & _ " method void decSize() {" & @CRLF & _ " if (size > 2) {" & @CRLF & _ " do erase();" & @CRLF & _ " let size = size - 2;" & @CRLF & _ " do draw();" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Moves the square up by 2 pixels. */" & @CRLF & _ " method void moveUp() {" & @CRLF & _ " if (y > 1) {" & @CRLF & _ " do Screen.setColor(false);" & @CRLF & _ " do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size);" & @CRLF & _ " let y = y - 2;" & @CRLF & _ " do Screen.setColor(true);" & @CRLF & _ " do Screen.drawRectangle(x, y, x + size, y + 1);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Moves the square down by 2 pixels. */" & @CRLF & _ " method void moveDown() {" & @CRLF & _ " if ((y + size) < 254) {" & @CRLF & _ " do Screen.setColor(false);" & @CRLF & _ " do Screen.drawRectangle(x, y, x + size, y + 1);" & @CRLF & _ " let y = y + 2;" & @CRLF & _ " do Screen.setColor(true);" & @CRLF & _ " do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Moves the square left by 2 pixels. */" & @CRLF & _ " method void moveLeft() {" & @CRLF & _ " if (x > 1) {" & @CRLF & _ " do Screen.setColor(false);" & @CRLF & _ " do Screen.drawRectangle((x + size) - 1, y, x + size, y + size);" & @CRLF & _ " let x = x - 2;" & @CRLF & _ " do Screen.setColor(true);" & @CRLF & _ " do Screen.drawRectangle(x, y, x + 1, y + size);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Moves the square right by 2 pixels. */" & @CRLF & _ " method void moveRight() {" & @CRLF & _ " if ((x + size) < 510) {" & @CRLF & _ " do Screen.setColor(false);" & @CRLF & _ " do Screen.drawRectangle(x, y, x + 1, y + size);" & @CRLF & _ " let x = x + 2;" & @CRLF & _ " do Screen.setColor(true);" & @CRLF & _ " do Screen.drawRectangle((x + size) - 1, y, x + size, y + size);" & @CRLF & _ " }" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "// This file is part of www.nand2tetris.org" & @CRLF & _ "// and the book "The Elements of Computing Systems"" & @CRLF & _ "// by Nisan and Schocken, MIT Press." & @CRLF & _ "// File name: projects/10/Square/SquareGame.jack" & @CRLF & _ "" & @CRLF & _ "// (same as projects/09/Square/SquareGame.jack)" & @CRLF & _ "" & @CRLF & _ "/**" & @CRLF & _ " * Implements the Square Dance game." & @CRLF & _ " * This simple game allows the user to move a black square around" & @CRLF & _ " * the screen, and change the square's size during the movement." & @CRLF & _ " * When the game starts, a square of 30 by 30 pixels is shown at the" & @CRLF & _ " * top-left corner of the screen. The user controls the square as follows." & @CRLF & _ " * The 4 arrow keys are used to move the square up, down, left, and right." & @CRLF & _ " * The 'z' and 'x' keys are used, respectively, to decrement and increment" & @CRLF & _ " * the square's size. The 'q' key is used to quit the game." & @CRLF & _ " */" & @CRLF & _ "" & @CRLF & _ "class SquareGame {" & @CRLF & _ " field Square square; // the square of this game" & @CRLF & _ " field int direction; // the square's current direction: " & @CRLF & _ " // 0=none, 1=up, 2=down, 3=left, 4=right" & @CRLF & _ "" & @CRLF & _ " /** Constructs a new Square Game. */" & @CRLF & _ " constructor SquareGame new() {" & @CRLF & _ " // Creates a 30 by 30 pixels square and positions it at the top-left" & @CRLF & _ " // of the screen." & @CRLF & _ " let square = Square.new(0, 0, 30);" & @CRLF & _ " let direction = 0; // initial state is no movement" & @CRLF & _ " return this;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Disposes this game. */" & @CRLF & _ " method void dispose() {" & @CRLF & _ " do square.dispose();" & @CRLF & _ " do Memory.deAlloc(this);" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Moves the square in the current direction. */" & @CRLF & _ " method void moveSquare() {" & @CRLF & _ " if (direction = 1) { do square.moveUp(); }" & @CRLF & _ " if (direction = 2) { do square.moveDown(); }" & @CRLF & _ " if (direction = 3) { do square.moveLeft(); }" & @CRLF & _ " if (direction = 4) { do square.moveRight(); }" & @CRLF & _ " do Sys.wait(5); // delays the next movement" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ " /** Runs the game: handles the user's inputs and moves the square accordingly */" & @CRLF & _ " method void run() {" & @CRLF & _ " var char key; // the key currently pressed by the user" & @CRLF & _ " var boolean exit;" & @CRLF & _ " let exit = false;" & @CRLF & _ " " & @CRLF & _ " while (~exit) {" & @CRLF & _ " // waits for a key to be pressed" & @CRLF & _ " while (key = 0) {" & @CRLF & _ " let key = Keyboard.keyPressed();" & @CRLF & _ " do moveSquare();" & @CRLF & _ " }" & @CRLF & _ " if (key = 81) { let exit = true; } // q key" & @CRLF & _ " if (key = 90) { do square.decSize(); } // z key" & @CRLF & _ " if (key = 88) { do square.incSize(); } // x key" & @CRLF & _ " if (key = 131) { let direction = 1; } // up arrow" & @CRLF & _ " if (key = 133) { let direction = 2; } // down arrow" & @CRLF & _ " if (key = 130) { let direction = 3; } // left arrow" & @CRLF & _ " if (key = 132) { let direction = 4; } // right arrow" & @CRLF & _ "" & @CRLF & _ " // waits for the key to be released" & @CRLF & _ " while (~(key = 0)) {" & @CRLF & _ " let key = Keyboard.keyPressed();" & @CRLF & _ " do moveSquare();" & @CRLF & _ " }" & @CRLF & _ " } // while" & @CRLF & _ " return;" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" 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