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

/
/
g

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 = "(((import java\.)[a-zA-Z\.\*]*;)|(public static void main(.*))|(class\s[a-zA-Z_$]+[\s]*\{)|(System\.out\.print))" Local $sString = "/*" & @CRLF & _ " Calculate Circle Area using Java Example" & @CRLF & _ " This Calculate Circle Area using Java Example shows how to calculate" & @CRLF & _ " area of circle using it's radius." & @CRLF & _ "*/" & @CRLF & _ " " & @CRLF & _ "import java.io.BufferedReader;" & @CRLF & _ "import java.io.IOException;" & @CRLF & _ "import java.io.InputStreamReader;" & @CRLF & _ " " & @CRLF & _ "public class CalculateCircleAreaExample {" & @CRLF & _ " " & @CRLF & _ " public static void main(String[] args) {" & @CRLF & _ " " & @CRLF & _ " int radius = 0;" & @CRLF & _ " System.out.println("Please enter radius of a circle");" & @CRLF & _ " " & @CRLF & _ " try" & @CRLF & _ " {" & @CRLF & _ " //get the radius from console" & @CRLF & _ " BufferedReader br = new BufferedReader(new InputStreamReader(System.in));" & @CRLF & _ " radius = Integer.parseInt(br.readLine());" & @CRLF & _ " }" & @CRLF & _ " //if invalid value was entered" & @CRLF & _ " catch(NumberFormatException ne)" & @CRLF & _ " {" & @CRLF & _ " System.out.println("Invalid radius value" + ne);" & @CRLF & _ " System.exit(0);" & @CRLF & _ " }" & @CRLF & _ " catch(IOException ioe)" & @CRLF & _ " {" & @CRLF & _ " System.out.println("IO Error :" + ioe);" & @CRLF & _ " System.exit(0);" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " /*" & @CRLF & _ " * Area of a circle is" & @CRLF & _ " * pi * r * r" & @CRLF & _ " * where r is a radius of a circle." & @CRLF & _ " */" & @CRLF & _ " " & @CRLF & _ " //NOTE : use Math.PI constant to get value of pi" & @CRLF & _ " double area = Math.PI * radius * radius;" & @CRLF & _ " " & @CRLF & _ " System.out.println("Area of a circle is " + area);" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ " " & @CRLF & _ "/*" & @CRLF & _ "Output of Calculate Circle Area using Java Example would be" & @CRLF & _ "Please enter radius of a circle" & @CRLF & _ "19" & @CRLF & _ "Area of a circle is 1134.1149479459152" & @CRLF & _ "*/" & @CRLF & _ "" & @CRLF & _ "@@@@" & @CRLF & _ "" & @CRLF & _ "/*" & @CRLF & _ " Calculate Rectangle Area using Java Example" & @CRLF & _ " This Calculate Rectangle Area using Java Example shows how to calculate" & @CRLF & _ " area of Rectangle using it's length and width." & @CRLF & _ "*/" & @CRLF & _ " " & @CRLF & _ "import java.io.BufferedReader;" & @CRLF & _ "import java.io.IOException;" & @CRLF & _ "import java.io.InputStreamReader;" & @CRLF & _ " " & @CRLF & _ "public class CalculateRectArea {" & @CRLF & _ " " & @CRLF & _ " public static void main(String[] args) {" & @CRLF & _ " " & @CRLF & _ " int width = 0;" & @CRLF & _ " int length = 0;" & @CRLF & _ " " & @CRLF & _ " try" & @CRLF & _ " {" & @CRLF & _ " //read the length from console" & @CRLF & _ " BufferedReader br = new BufferedReader(new InputStreamReader(System.in));" & @CRLF & _ " " & @CRLF & _ " System.out.println("Please enter length of a rectangle");" & @CRLF & _ " length = Integer.parseInt(br.readLine());" & @CRLF & _ " " & @CRLF & _ " //read the width from console" & @CRLF & _ " System.out.println("Please enter width of a rectangle");" & @CRLF & _ " width = Integer.parseInt(br.readLine());" & @CRLF & _ " " & @CRLF & _ " " & @CRLF & _ " }" & @CRLF & _ " //if invalid value was entered" & @CRLF & _ " catch(NumberFormatException ne)" & @CRLF & _ " {" & @CRLF & _ " System.out.println("Invalid value" + ne);" & @CRLF & _ " System.exit(0);" & @CRLF & _ " }" & @CRLF & _ " catch(IOException ioe)" & @CRLF & _ " {" & @CRLF & _ " System.out.println("IO Error :" + ioe);" & @CRLF & _ " System.exit(0);" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " /*" & @CRLF & _ " * Area of a rectangle is" & @CRLF & _ " * length * width" & @CRLF & _ " */" & @CRLF & _ " " & @CRLF & _ " int area = length * width;" & @CRLF & _ " " & @CRLF & _ " System.out.println("Area of a rectangle is " + area);" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ "}" & @CRLF & _ " " & @CRLF & _ "/*" & @CRLF & _ "Output of Calculate Rectangle Area using Java Example would be" & @CRLF & _ "Please enter length of a rectangle" & @CRLF & _ "10" & @CRLF & _ "Please enter width of a rectangle" & @CRLF & _ "15" & @CRLF & _ "Area of a rectangle is 150" & @CRLF & _ "*/" & @CRLF & _ "" & @CRLF & _ "@@@@" & @CRLF & _ "" & @CRLF & _ "/*" & @CRLF & _ " Java Factorial Using Recursion Example" & @CRLF & _ " This Java example shows how to generate factorial of a given number" & @CRLF & _ " using recursive function." & @CRLF & _ "*/" & @CRLF & _ " " & @CRLF & _ "import java.io.BufferedReader;" & @CRLF & _ "import java.io.IOException;" & @CRLF & _ "import java.io.InputStreamReader;" & @CRLF & _ " " & @CRLF & _ "public class JavaFactorialUsingRecursion {" & @CRLF & _ " " & @CRLF & _ " public static void main(String args[]) throws NumberFormatException, IOException{" & @CRLF & _ " " & @CRLF & _ " System.out.println("Enter the number: ");" & @CRLF & _ " " & @CRLF & _ " //get input from the user" & @CRLF & _ " BufferedReader br=new BufferedReader(new InputStreamReader(System.in));" & @CRLF & _ " int a = Integer.parseInt(br.readLine());" & @CRLF & _ " " & @CRLF & _ " //call the recursive function to generate factorial" & @CRLF & _ " int result= fact(a);" & @CRLF & _ " " & @CRLF & _ " " & @CRLF & _ " System.out.println("Factorial of the number is: " + result);" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " static int fact(int b)" & @CRLF & _ " {" & @CRLF & _ " if(b <= 1)" & @CRLF & _ " //if the number is 1 then return 1" & @CRLF & _ " return 1;" & @CRLF & _ " else" & @CRLF & _ " //else call the same function with the value - 1" & @CRLF & _ " return b * fact(b-1);" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ " " & @CRLF & _ "/*" & @CRLF & _ "Output of this Java example would be" & @CRLF & _ " " & @CRLF & _ "Enter the number:" & @CRLF & _ "5" & @CRLF & _ "Factorial of the number is: 120" & @CRLF & _ "*/" & @CRLF & _ "" & @CRLF & _ "@@@@" & @CRLF & _ "" & @CRLF & _ "/*" & @CRLF & _ " Swap Numbers Without Using Third Variable Java Example" & @CRLF & _ " This Swap Numbers Java Example shows how to" & @CRLF & _ " swap value of two numbers without using third variable using java." & @CRLF & _ "*/" & @CRLF & _ " " & @CRLF & _ "public class SwapElementsWithoutThirdVariableExample {" & @CRLF & _ " " & @CRLF & _ " public static void main(String[] args) {" & @CRLF & _ " " & @CRLF & _ " int num1 = 10;" & @CRLF & _ " int num2 = 20;" & @CRLF & _ " " & @CRLF & _ " System.out.println("Before Swapping");" & @CRLF & _ " System.out.println("Value of num1 is :" + num1);" & @CRLF & _ " System.out.println("Value of num2 is :" +num2);" & @CRLF & _ " " & @CRLF & _ " //add both the numbers and assign it to first" & @CRLF & _ " num1 = num1 + num2;" & @CRLF & _ " num2 = num1 - num2;" & @CRLF & _ " num1 = num1 - num2;" & @CRLF & _ " " & @CRLF & _ " System.out.println("Before Swapping");" & @CRLF & _ " System.out.println("Value of num1 is :" + num1);" & @CRLF & _ " System.out.println("Value of num2 is :" +num2);" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " " & @CRLF & _ "}" & @CRLF & _ " " & @CRLF & _ "/*" & @CRLF & _ "Output of Swap Numbers Without Using Third Variable example would be" & @CRLF & _ "Before Swapping" & @CRLF & _ "Value of num1 is :10" & @CRLF & _ "Value of num2 is :20" & @CRLF & _ "Before Swapping" & @CRLF & _ "Value of num1 is :20" & @CRLF & _ "Value of num2 is :10" & @CRLF & _ "*/" & @CRLF & _ "" & @CRLF & _ "@@@@" & @CRLF & _ "" & @CRLF & _ "// OddEven.java" & @CRLF & _ "import javax.swing.JOptionPane;" & @CRLF & _ " " & @CRLF & _ "public class OddEven {" & @CRLF & _ " /**" & @CRLF & _ " * "input" is the number that the user gives to the computer" & @CRLF & _ " */" & @CRLF & _ " private int input; // a whole number("int" means integer)" & @CRLF & _ " " & @CRLF & _ " /**" & @CRLF & _ " * This is the constructor method. It gets called when an object of the OddEven type" & @CRLF & _ " * is being created." & @CRLF & _ " */" & @CRLF & _ " public OddEven() {" & @CRLF & _ " /*" & @CRLF & _ " * In most Java programs constructors can initialize objects with default values, or create" & @CRLF & _ " * other objects that this object might use to perform its functions. In some Java programs, the" & @CRLF & _ " * constructor may simply be an empty function if nothing needs to be initialized prior to the" & @CRLF & _ " * functioning of the object. In this program's case, an empty constructor would suffice." & @CRLF & _ " * A constructor must exist; however, if the user doesn't put one in then the compiler" & @CRLF & _ " * will create an empty one." & @CRLF & _ " */" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " /**" & @CRLF & _ " * This is the main method. It gets called when this class is run through a Java interpreter." & @CRLF & _ " * @param args command line arguments (unused)" & @CRLF & _ " */" & @CRLF & _ " public static void main(final String[] args) {" & @CRLF & _ " /*" & @CRLF & _ " * This line of code creates a new instance of this class called "number" (also known as an" & @CRLF & _ " * Object) and initializes it by calling the constructor. The next line of code calls" & @CRLF & _ " * the "showDialog()" method, which brings up a prompt to ask you for a number" & @CRLF & _ " */" & @CRLF & _ " OddEven number = new OddEven();" & @CRLF & _ " number.showDialog();" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " public void showDialog() {" & @CRLF & _ " /*" & @CRLF & _ " * "try" makes sure nothing goes wrong. If something does," & @CRLF & _ " * the interpreter skips to "catch" to see what it should do." & @CRLF & _ " */" & @CRLF & _ " try {" & @CRLF & _ " /*" & @CRLF & _ " * The code below brings up a JOptionPane, which is a dialog box" & @CRLF & _ " * The String returned by the "showInputDialog()" method is converted into" & @CRLF & _ " * an integer, making the program treat it as a number instead of a word." & @CRLF & _ " * After that, this method calls a second method, calculate() that will" & @CRLF & _ " * display either "Even" or "Odd."" & @CRLF & _ " */" & @CRLF & _ " this.input = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number."));" & @CRLF & _ " this.calculate();" & @CRLF & _ " } catch (final NumberFormatException e) {" & @CRLF & _ " /*" & @CRLF & _ " * Getting in the catch block means that there was a problem with the format of" & @CRLF & _ " * the number. Probably some letters were typed in instead of a number." & @CRLF & _ " */" & @CRLF & _ " System.err.println("ERROR: Invalid input. Please type in a numerical value.");" & @CRLF & _ " }" & @CRLF & _ " }" & @CRLF & _ " " & @CRLF & _ " /**" & @CRLF & _ " * When this gets called, it sends a message to the interpreter." & @CRLF & _ " * The interpreter usually shows it on the command prompt (For Windows users)" & @CRLF & _ " * or the terminal (For *nix users).(Assuming it's open)" & @CRLF & _ " */" & @CRLF & _ " private void calculate() {" & @CRLF & _ " if ((this.input % 2) == 0) {" & @CRLF & _ " JOptionPane.showMessageDialog(null, "Even");" & @CRLF & _ " } else {" & @CRLF & _ " JOptionPane.showMessageDialog(null, "Odd");" & @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