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

package main import ( "regexp" "fmt" ) func main() { var re = regexp.MustCompile(`(?m)l4.2`) var str = `// Lab 4.2 #include <iostream> #include <string> using namespace std; string s1, s2; int checkChar() { int i, len; len = s2.length(); for (i = 0; i < len; i++) if (s2[i] - 48 < 0 || s2[i] - 48 > 9) return 0; return 1; } void inputString() { cout << " - Nhap chuoi S1: "; getline(cin, s1); while (1) { cout << " - Nhap chuoi S2: "; getline(cin, s2); if (!checkChar()) cout << " => " << "Input Validation" << endl << endl; else break; } } int checkNumber() { int i, len; len = s1.length(); for (i = 0; i < len; i++) if (s1[i] - 48 >= 0 && s1[i] - 48 <= 9) return 1; return 0; } int checkSDTMobifone() { int i, len; len = s2.length(); if (len==10 && s2[0] - 48 == 0 && s2[1] - 48 == 9 && s2[2] - 48 == 0) { for (i = 3; i < 10; i++) { if (s2[i] - 48 < 0 || s2[i] - 48 > 9) return 0; } return 1; } else return 0; } int checkSubString() { int i, j; int len, len_s1, len_s2, flag; char temp; len_s1 = s1.length(); len_s2 = s2.length(); if (len_s2 == 0) return 1; if (len_s1 >= len_s2) { for (i = 0; i <= len_s1 - len_s2; i++) { flag = 1; for (j = 0; flag&&j < len_s2; j++) { if (s1[i + j] != s2[j]) flag = 0; } if (flag) return 1; } } return 0; } string addString(){ string s3; int i, len_s1, len_s2, len; len_s1 = s1.length(); len_s2 = s2.length(); len = len_s1 + len_s2; for (i = 0; i < len; i++) { if (i < len_s1) s3 += s1[i]; else s3 += s2[i - len_s1]; } return s3; } void insertString(string s3) { string str_Insert = "Buffer_Overflow"; char after; int i, len, flag; len = s3.length(); cout << " - Chuoi S3: " << s3 << endl; cout << " - Ban muon them vao sau ky tu nao: "; cin >> after; cout << endl << " => "; flag = 0; for (i = 0; i < len; i++) { if (s3[i] != after) { cout << s3[i]; } else { cout << after << str_Insert; flag++; } } if (!flag) cout << str_Insert << endl; } int main() { string s1, s2, s3; // Cau 1 cout << endl << "Cau 1: " << endl; inputString(); // Cau 2 cout << endl << "Cau 2: " << endl; if (checkNumber()) cout << " - Chuoi S1 co chu so" << endl; else cout << " - Chuoi S1 khong co chu so" << endl; // Cau 3 cout << endl << "Cau 3: " << endl; if (checkSDTMobifone()) cout << " - Chuoi S2 la SDT mang Mobifone" << endl; else cout << " - Chuoi S2 khong la SDT mang Mobifone" << endl; // Cau 4 cout << endl << "Cau 4: " << endl; if (checkSubString()) cout << " - Chuoi S2 la tap con cua chuoi S1" << endl; else cout << " - Chuoi S2 khong la tap con cua chuoi S1" << endl; // Cau 5 cout << endl << "Cau 5: " << endl; cout << " - Chuoi S3 = S1 + S2: "; s3 = addString(); cout << s3 << endl; // Cau 6 cout << endl << "Cau 6: " << endl; insertString(s3); cout << endl << endl; //system("pause"); return 0; } ` for i, match := range re.FindAllString(str, -1) { fmt.Println(match, "found at index", 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 Golang, please visit: https://golang.org/pkg/regexp/