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.2"; final String string = "// Lab 4.2\n" + "#include <iostream>\n" + "#include <string>\n\n" + "using namespace std;\n\n" + "string s1, s2;\n\n" + "int checkChar() {\n" + " int i, len;\n\n" + " len = s2.length();\n" + " for (i = 0; i < len; i++)\n" + " if (s2[i] - 48 < 0 || s2[i] - 48 > 9)\n" + " return 0;\n\n" + " return 1;\n" + "}\n\n" + "void inputString() {\n" + " cout << \" - Nhap chuoi S1: \";\n" + " getline(cin, s1);\n" + " \n" + " while (1) {\n" + " cout << \" - Nhap chuoi S2: \";\n" + " getline(cin, s2);\n\n" + " if (!checkChar())\n" + " cout << \" => \" << \"Input Validation\" << endl << endl;\n" + " else\n" + " break;\n" + " }\n" + "}\n\n" + "int checkNumber() {\n" + " int i, len;\n" + " len = s1.length();\n\n" + " for (i = 0; i < len; i++)\n" + " if (s1[i] - 48 >= 0 && s1[i] - 48 <= 9)\n" + " return 1;\n\n" + " return 0;\n" + "}\n\n" + "int checkSDTMobifone() {\n" + " int i, len;\n" + " len = s2.length();\n\n" + " if (len==10 && s2[0] - 48 == 0 && s2[1] - 48 == 9 && s2[2] - 48 == 0) {\n" + " for (i = 3; i < 10; i++) {\n" + " if (s2[i] - 48 < 0 || s2[i] - 48 > 9)\n" + " return 0;\n" + " }\n" + " return 1;\n" + " }\n" + " else\n" + " return 0;\n" + "}\n\n" + "int checkSubString() {\n" + " int i, j;\n" + " int len, len_s1, len_s2, flag;\n" + " char temp;\n\n" + " len_s1 = s1.length();\n" + " len_s2 = s2.length();\n\n" + " if (len_s2 == 0)\n" + " return 1;\n\n" + " if (len_s1 >= len_s2) {\n" + " for (i = 0; i <= len_s1 - len_s2; i++) {\n" + " flag = 1;\n" + " for (j = 0; flag&&j < len_s2; j++) {\n" + " if (s1[i + j] != s2[j])\n" + " flag = 0;\n" + " }\n" + " if (flag)\n" + " return 1;\n" + " }\n" + " }\n" + " \n" + " return 0;\n" + "}\n\n" + "string addString(){\n" + " string s3;\n" + " int i, len_s1, len_s2, len;\n\n" + " len_s1 = s1.length();\n" + " len_s2 = s2.length();\n" + " len = len_s1 + len_s2;\n\n" + " for (i = 0; i < len; i++) {\n" + " if (i < len_s1)\n" + " s3 += s1[i];\n" + " else\n" + " s3 += s2[i - len_s1];\n" + " }\n\n" + " return s3;\n" + "}\n\n" + "void insertString(string s3) {\n" + " string str_Insert = \"Buffer_Overflow\";\n" + " char after;\n" + " int i, len, flag;\n\n" + " len = s3.length();\n\n" + " cout << \" - Chuoi S3: \" << s3 << endl;\n\n" + " cout << \" - Ban muon them vao sau ky tu nao: \";\n" + " cin >> after;\n\n" + " cout << endl << \" => \";\n\n" + " flag = 0;\n" + " for (i = 0; i < len; i++) {\n" + " if (s3[i] != after) {\n" + " cout << s3[i];\n" + " }\n" + " else {\n" + " cout << after << str_Insert;\n" + " flag++;\n" + " }\n" + " }\n\n" + " if (!flag)\n" + " cout << str_Insert << endl;\n" + "}\n\n" + "int main() {\n\n" + " string s1, s2, s3;\n" + " // Cau 1\n" + " cout << endl << \"Cau 1: \" << endl;\n" + " inputString();\n\n" + " // Cau 2\n" + " cout << endl << \"Cau 2: \" << endl;\n" + " if (checkNumber())\n" + " cout << \" - Chuoi S1 co chu so\" << endl;\n" + " else\n" + " cout << \" - Chuoi S1 khong co chu so\" << endl;\n\n" + " // Cau 3\n" + " cout << endl << \"Cau 3: \" << endl;\n" + " if (checkSDTMobifone())\n" + " cout << \" - Chuoi S2 la SDT mang Mobifone\" << endl;\n" + " else\n" + " cout << \" - Chuoi S2 khong la SDT mang Mobifone\" << endl;\n\n" + " // Cau 4\n" + " cout << endl << \"Cau 4: \" << endl;\n" + " if (checkSubString())\n" + " cout << \" - Chuoi S2 la tap con cua chuoi S1\" << endl;\n" + " else\n" + " cout << \" - Chuoi S2 khong la tap con cua chuoi S1\" << endl;\n\n" + " // Cau 5\n" + " cout << endl << \"Cau 5: \" << endl;\n" + " cout << \" - Chuoi S3 = S1 + S2: \";\n" + " s3 = addString();\n" + " cout << s3 << endl;\n\n" + " // Cau 6\n" + " cout << endl << \"Cau 6: \" << endl;\n" + " insertString(s3);\n" + " cout << endl << endl;\n\n\n" + " //system(\"pause\");\n" + " \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