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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "l4.1"; final String string = "// Lab 4.1\n\n\n" + "#include <iostream>\n" + "#include <stdlib.h>\n\n" + "using namespace std;\n\n" + "int n, m;\n" + "int a[21], b[21], c[42];\n\n" + "// Cau 1\n" + "void createArray() {\n" + " int i;\n\n" + " do {\n" + " cout << \" - Nhap gia tri cua n: \";\n" + " cin >> n;\n" + " } while (10 > n || n > 20);\n\n" + " // Khoi tao 2 mang A & B co n phan tu\n" + " for (i = 0; i < n; i++) {\n" + " a[i] = rand() % 490 + 10;\n" + " b[i] = rand() % 490 + 10;\n" + " }\n" + "}\n\n" + "void printArray(int *array, int len) {\n" + " int i;\n\n" + " for (i = 0; i < len; i++) {\n" + " cout << \"\\t\" << array[i];\n" + " }\n" + " cout << endl;\n" + "}\n\n" + "void chanA_LeB() {\n" + " int i, j;\n" + " j = 0;\n" + " for (i = 0; i < n; i++) {\n" + " if (a[i] % 2 == 0) {\n" + " c[j] = a[i];\n" + " j++;\n" + " }\n" + " if (b[i] % 2 != 0) {\n" + " c[j] = b[i];\n" + " j++;\n" + " }\n" + " }\n" + " m = j;\n" + "}\n\n" + "int mul_is_safe(int a, int b) {\n" + " if (b > INT_MAX / a)\n" + " return 0;\n" + " return 1;\n" + "}\n\n" + "void mulArray() {\n" + " int i, j;\n" + " long long KQ;\n" + " KQ = 1;\n\n" + " for (i = 0; i < m; i++) {\n" + " if (mul_is_safe(c[i], KQ)) {\n" + " cout << c[i];\n" + " KQ *= c[i];\n" + " for (j = i-1; j >= 0; j--) {\n" + " if (i != 0)\n" + " cout << \" * \" << c[j];\n" + " }\n" + " cout << \" = \" << KQ << endl;\n" + " }\n" + " else {\n" + " cout << \"Integer Overflow\" << endl << endl;\n" + " return;\n" + " }\n" + " }\n" + "}\n\n" + "int main() {\n\n" + " // Cau 1\n" + " cout << \"Cau 1: \" << endl;\n" + " createArray();\n\n" + " // Cau 2\n" + " cout << endl << \"Cau 2: \" << endl;\n" + " cout << \" - Mang A: \" << endl;\n" + " printArray(a, n);\n" + " cout << \" - Mang B: \" << endl;\n" + " printArray(b, n);\n\n" + " // Cau 3\n" + " cout << endl << \"Cau 3: \" << endl;\n" + " chanA_LeB();\n" + " cout << \" - Mang C: \" << endl;\n" + " printArray(c, m);\n\n" + " // Cau 4\n" + " cout << endl << \"Cau 4: \" << endl;\n" + " mulArray();\n\n" + " //system(\"pause\");\n" + " return 0;\n" + "}\n"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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