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

Substitution

Processing...

Code Generator

Generated Code

re = /(\(\)\n[[:space:]]{1,}\{)|(\(\)[[:space:]]{1,}\{)/m str = '// feedback thread import java.util.*; class Main { static Scanner scan = new Scanner(System.in); static String deadEnd = "dead end--morphing backwards!"; static String gettingCloser = "You\'r getting closer"; // call this method after each decision has been made public static void clearScreen() { Scanner scan = new Scanner(System.in); System.out.println("Press enter to continue."); String x = scan.nextLine(); System.out.print("\\033[H\\033[2J"); System.out.flush(); } public static void decisionZero() { System.out.println("Decision Point 0"); System.out.println("Choose a direction Mr. Mouse"); String direction = scan.nextLine(); switch (direction.toLowerCase()) { case "n": System.out.println("You chose north."); System.out.println(deadEnd); decisionZero(); break; case "s": System.out.println("You chose south."); System.out.println(gettingCloser); decisionOne(); break; case "e": System.out.println("You chose east."); System.out.println(deadEnd); decisionZero(); break; case "w": System.out.println("You chose west."); System.out.println(deadEnd); decisionZero(); break; } } public static void decisionOne() { System.out.println("Decision Point 1"); System.out.println("Choose a direction Mr. Mouse"); String direction = scan.nextLine(); switch (direction.toLowerCase()) { case "n": System.out.println("You chose north."); System.out.println(deadEnd); decisionZero(); break; case "s": System.out.println("You chose south."); System.out.println(deadEnd); decisionZero(); break; case "e": System.out.println("You chose east."); System.out.println(deadEnd); decisionZero(); break; case "w": System.out.println("You chose west."); System.out.println(gettingCloser); decisionTwo(); break; } } public static void decisionTwo() { System.out.println("Decision Point 2"); System.out.println("Choose a direction Mr. Mouse"); String direction = scan.nextLine(); int rndDecion = (int)(Math.random()*2); switch (direction.toLowerCase()) { case "n": System.out.println("You chose north."); System.out.println(gettingCloser); decisionThree(); break; case "s": System.out.println("You chose south."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); } break; case "e": System.out.println("You chose east."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); } break; case "w": System.out.println("You chose west."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); } break; } } public static void decisionThree() { System.out.println("Decision Point 3"); System.out.println("Choose a direction Mr. Mouse"); String direction = scan.nextLine(); int rndDecion = (int)(Math.random()*3); switch (direction.toLowerCase()) { case "n": System.out.println("You chose north."); System.out.println(gettingCloser); decisionFour(); break; case "s": System.out.println("You chose south."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); } break; case "e": System.out.println("You chose east."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); } break; case "w": System.out.println("You chose west."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); } break; } } public static void decisionFour() { System.out.println("Decision Point 4"); System.out.println("Choose a direction Mr. Mouse"); String direction = scan.nextLine(); int rndDecion = (int)(Math.random()*4); switch (direction.toLowerCase()) { case "n": System.out.println("You chose north."); System.out.println(gettingCloser); decisionFive(); break; case "s": System.out.println("You chose south."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); } break; case "e": System.out.println("You chose east."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); } break; case "w": System.out.println("You chose west."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); } break; } } public static void decisionFive() { System.out.println("Decision Point 5"); System.out.println("Choose a direction Mr. Mouse"); String direction = scan.nextLine(); int rndDecion = (int)(Math.random()*5); switch (direction.toLowerCase()) { case "n": System.out.println("You chose north."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); case 4: decisionFour(); } break; case "s": System.out.println("You chose south."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); case 4: decisionFour(); } break; case "e": System.out.println("You chose east."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); case 4: decisionFour(); } break; case "w": System.out.println("You chose west."); System.out.println(gettingCloser); decisionSix(); break; } } public static void decisionSix() { System.out.println("Decision Point 6"); System.out.println("Choose a direction Mr. Mouse"); String direction = scan.nextLine(); int rndDecion = (int)(Math.random()*5); switch (direction.toLowerCase()) { case "n": System.out.println("You chose north."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); case 4: decisionFour(); } break; case "s": System.out.println("You chose south."); System.out.println(deadEnd); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); case 4: decisionFour(); } break; case "e": System.out.println("You chose east."); System.out.println("Victory cheese!"); break; case "w": System.out.println("You chose west."); System.out.println(gettingCloser); switch (rndDecion) { case 0: decisionZero(); case 1: decisionOne(); case 2: decisionTwo(); case 3: decisionThree(); case 4: decisionFour(); } break; } } // the main method is COMPLETE. Add/change nothing here. public static void main(String[] args) { decisionZero(); //only line in main method. } }' subst = '()\\n {\\n clearScreen();' result = str.gsub(re, subst) # Print the result of the substitution puts 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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html