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

$re = '/l4.1/m'; $str = '// Lab 4.1 #include <iostream> #include <stdlib.h> using namespace std; int n, m; int a[21], b[21], c[42]; // Cau 1 void createArray() { int i; do { cout << " - Nhap gia tri cua n: "; cin >> n; } while (10 > n || n > 20); // Khoi tao 2 mang A & B co n phan tu for (i = 0; i < n; i++) { a[i] = rand() % 490 + 10; b[i] = rand() % 490 + 10; } } void printArray(int *array, int len) { int i; for (i = 0; i < len; i++) { cout << "\\t" << array[i]; } cout << endl; } void chanA_LeB() { int i, j; j = 0; for (i = 0; i < n; i++) { if (a[i] % 2 == 0) { c[j] = a[i]; j++; } if (b[i] % 2 != 0) { c[j] = b[i]; j++; } } m = j; } int mul_is_safe(int a, int b) { if (b > INT_MAX / a) return 0; return 1; } void mulArray() { int i, j; long long KQ; KQ = 1; for (i = 0; i < m; i++) { if (mul_is_safe(c[i], KQ)) { cout << c[i]; KQ *= c[i]; for (j = i-1; j >= 0; j--) { if (i != 0) cout << " * " << c[j]; } cout << " = " << KQ << endl; } else { cout << "Integer Overflow" << endl << endl; return; } } } int main() { // Cau 1 cout << "Cau 1: " << endl; createArray(); // Cau 2 cout << endl << "Cau 2: " << endl; cout << " - Mang A: " << endl; printArray(a, n); cout << " - Mang B: " << endl; printArray(b, n); // Cau 3 cout << endl << "Cau 3: " << endl; chanA_LeB(); cout << " - Mang C: " << endl; printArray(c, m); // Cau 4 cout << endl << "Cau 4: " << endl; mulArray(); //system("pause"); return 0; } '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php