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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "(\\(\\)\\n[[:space:]]{1,}\\{)|(\\(\\)[[:space:]]{1,}\\{)"; final String string = "// feedback thread\n\n" + "import java.util.*;\n\n" + "class Main\n" + "{\n" + " static Scanner scan = new Scanner(System.in);\n" + " static String deadEnd = \"dead end--morphing backwards!\";\n" + " static String gettingCloser = \"You'r getting closer\";\n" + " \n" + " // call this method after each decision has been made\n" + " public static void clearScreen() {\n" + " Scanner scan = new Scanner(System.in);\n" + " System.out.println(\"Press enter to continue.\");\n" + " String x = scan.nextLine(); \n" + " System.out.print(\"\\033[H\\033[2J\"); \n" + " System.out.flush(); \n" + " } \n\n\n\n" + " public static void decisionZero()\n" + " {\n" + " System.out.println(\"Decision Point 0\");\n" + " System.out.println(\"Choose a direction Mr. Mouse\");\n" + " String direction = scan.nextLine();\n" + " switch (direction.toLowerCase()) {\n" + " case \"n\":\n" + " System.out.println(\"You chose north.\");\n" + " System.out.println(deadEnd);\n" + " decisionZero();\n" + " break;\n\n" + " case \"s\":\n" + " System.out.println(\"You chose south.\");\n" + " System.out.println(gettingCloser);\n" + " decisionOne();\n" + " break;\n" + " \n" + " case \"e\":\n" + " System.out.println(\"You chose east.\");\n" + " System.out.println(deadEnd);\n" + " decisionZero();\n" + " break;\n\n" + " case \"w\":\n" + " System.out.println(\"You chose west.\");\n" + " System.out.println(deadEnd);\n" + " decisionZero();\n" + " break;\n" + " \n" + " }\n" + " }\n" + " \n" + " public static void decisionOne()\n" + " {\n" + " System.out.println(\"Decision Point 1\");\n" + " System.out.println(\"Choose a direction Mr. Mouse\");\n" + " String direction = scan.nextLine();\n" + " switch (direction.toLowerCase()) {\n" + " case \"n\":\n" + " System.out.println(\"You chose north.\");\n" + " System.out.println(deadEnd);\n" + " decisionZero();\n" + " break;\n\n" + " case \"s\":\n" + " System.out.println(\"You chose south.\");\n" + " System.out.println(deadEnd);\n" + " decisionZero();\n" + " break;\n\n" + " case \"e\":\n" + " System.out.println(\"You chose east.\");\n" + " System.out.println(deadEnd);\n" + " decisionZero();\n" + " break;\n\n" + " case \"w\":\n" + " System.out.println(\"You chose west.\");\n" + " System.out.println(gettingCloser);\n" + " decisionTwo();\n" + " break;\n\n" + " }\n" + " }\n" + " \n" + " public static void decisionTwo()\n" + " {\n" + " System.out.println(\"Decision Point 2\");\n" + " System.out.println(\"Choose a direction Mr. Mouse\");\n" + " String direction = scan.nextLine();\n" + " int rndDecion = (int)(Math.random()*2);\n" + " switch (direction.toLowerCase()) {\n" + " case \"n\":\n" + " System.out.println(\"You chose north.\");\n" + " System.out.println(gettingCloser);\n" + " decisionThree();\n" + " break;\n\n" + " case \"s\":\n" + " System.out.println(\"You chose south.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " }\n" + " break;\n\n" + " case \"e\":\n" + " System.out.println(\"You chose east.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " }\n" + " break;\n\n" + " case \"w\":\n" + " System.out.println(\"You chose west.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " }\n" + " break;\n\n" + " }\n" + " }\n" + " \n" + " public static void decisionThree()\n" + " {\n" + " System.out.println(\"Decision Point 3\");\n" + " System.out.println(\"Choose a direction Mr. Mouse\");\n" + " String direction = scan.nextLine();\n" + " int rndDecion = (int)(Math.random()*3);\n" + " switch (direction.toLowerCase()) {\n" + " case \"n\":\n" + " System.out.println(\"You chose north.\");\n" + " System.out.println(gettingCloser);\n" + " decisionFour();\n" + " break;\n\n" + " case \"s\":\n" + " System.out.println(\"You chose south.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " }\n" + " break;\n\n" + " case \"e\":\n" + " System.out.println(\"You chose east.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " }\n" + " break;\n\n" + " case \"w\":\n" + " System.out.println(\"You chose west.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " }\n" + " break;\n\n" + " }\n" + " }\n" + " \n" + " public static void decisionFour()\n" + " {\n" + " System.out.println(\"Decision Point 4\");\n" + " System.out.println(\"Choose a direction Mr. Mouse\");\n" + " String direction = scan.nextLine();\n" + " int rndDecion = (int)(Math.random()*4);\n" + " switch (direction.toLowerCase()) {\n" + " case \"n\":\n" + " System.out.println(\"You chose north.\");\n" + " System.out.println(gettingCloser);\n" + " decisionFive();\n" + " break;\n\n" + " case \"s\":\n" + " System.out.println(\"You chose south.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " }\n" + " break;\n\n" + " case \"e\":\n" + " System.out.println(\"You chose east.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " }\n" + " break;\n\n" + " case \"w\":\n" + " System.out.println(\"You chose west.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " }\n" + " break;\n\n" + " }\n" + " }\n\n" + " public static void decisionFive()\n" + " {\n" + " System.out.println(\"Decision Point 5\");\n" + " System.out.println(\"Choose a direction Mr. Mouse\");\n" + " String direction = scan.nextLine();\n" + " int rndDecion = (int)(Math.random()*5);\n" + " switch (direction.toLowerCase()) {\n" + " case \"n\":\n" + " System.out.println(\"You chose north.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " case 4:\n" + " decisionFour();\n" + " }\n" + " break;\n\n" + " case \"s\":\n" + " System.out.println(\"You chose south.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " case 4:\n" + " decisionFour();\n" + " }\n" + " break;\n\n" + " case \"e\":\n" + " System.out.println(\"You chose east.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " case 4:\n" + " decisionFour();\n" + " }\n" + " break;\n\n" + " case \"w\":\n" + " System.out.println(\"You chose west.\");\n" + " System.out.println(gettingCloser);\n" + " decisionSix();\n" + " break;\n\n" + " }\n" + " }\n\n" + " public static void decisionSix()\n" + " {\n" + " System.out.println(\"Decision Point 6\");\n" + " System.out.println(\"Choose a direction Mr. Mouse\");\n" + " String direction = scan.nextLine();\n" + " int rndDecion = (int)(Math.random()*5);\n" + " switch (direction.toLowerCase()) {\n" + " case \"n\":\n" + " System.out.println(\"You chose north.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " case 4:\n" + " decisionFour();\n" + " }\n" + " break;\n\n" + " case \"s\":\n" + " System.out.println(\"You chose south.\");\n" + " System.out.println(deadEnd);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " case 4:\n" + " decisionFour();\n" + " }\n" + " break;\n\n" + " case \"e\":\n" + " System.out.println(\"You chose east.\");\n" + " System.out.println(\"Victory cheese!\");\n" + " break;\n\n" + " case \"w\":\n" + " System.out.println(\"You chose west.\");\n" + " System.out.println(gettingCloser);\n" + " switch (rndDecion) {\n" + " case 0:\n" + " decisionZero();\n" + " case 1:\n" + " decisionOne();\n" + " case 2:\n" + " decisionTwo();\n" + " case 3:\n" + " decisionThree();\n" + " case 4:\n" + " decisionFour();\n" + " }\n" + " break;\n\n" + " }\n" + " }\n\n\n" + " // the main method is COMPLETE. Add/change nothing here.\n" + " public static void main(String[] args) \n" + " {\n" + " decisionZero(); //only line in main method.\n" + " }\n\n\n\n" + "}"; final String subst = "()\\n {\\n clearScreen();"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); // The substituted value will be contained in the result variable final String result = matcher.replaceAll(subst); System.out.println("Substitution result: " + 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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html