Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
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
Processing...

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 = "kthpppp"; final String string = "//\n" + "///*\n" + "//\n" + "//// Bai 2\n" + "//#include <iostream>\n" + "//\n" + "//using namespace std;\n" + "//\n" + "//int a, b;\n" + "//long long c;\n" + "//\n" + "//void init() {\n" + "// cout << \"Khoi tao: \" << endl;\n" + "// do {\n" + "// cout << \" - Nhap vao gia tri cua a: \";\n" + "// cin >> a;\n" + "// } while (10 > a || a > 50);\n" + "// \n" + "// do {\n" + "// cout << \" - Nhap vao gia tri cua b: \";\n" + "// cin >> b;\n" + "// } while (10 > b || b > 50 || a>=b);\n" + "//\n" + "// c = a*b;\n" + "//}\n" + "//\n" + "//void timUoc(int a) {\n" + "// int i;\n" + "// for (i = 1; i < a ; i++) {\n" + "// if (a%i == 0) {\n" + "// cout << \" \" << i;\n" + "// }\n" + "// }\n" + "// cout << endl;\n" + "//}\n" + "//\n" + "//int checkPP(int a) {\n" + "// int i;\n" + "// long sum;\n" + "// sum = 1;\n" + "//\n" + "// for (i = 2; i < a ; i++) {\n" + "// if (a%i == 0) {\n" + "// sum += i;\n" + "// }\n" + "// }\n" + "//\n" + "// if (sum > a)\n" + "// return 1;\n" + "// return 0;\n" + "//}\n" + "//\n" + "//\n" + "//int main() {\n" + "//\n" + "// init();\n" + "//\n" + "// // Cau 1\n" + "// cout << endl << \"Cau 1: \" << endl;\n" + "// timUoc(c);\n" + "//\n" + "// // Cau 2\n" + "// cout << endl << \"Cau 2: \" << endl;\n" + "// cout << \" - C = \" << c << endl;\n" + "// if (checkPP(c))\n" + "// cout << \" - \" << c << \" la so phong phu\" << endl;\n" + "// else\n" + "// cout << \" - \" << c << \" khong la so phong phu\" << endl;\n" + "//\n" + "// // Cau 3\n" + "// cout << endl << \"Cau 3: \" << endl;\n" + "// for (int i = a; i <= b; i++) {\n" + "// if (checkPP(i))\n" + "// cout << \" \" << i;\n" + "// }\n" + "//\n" + "// cout << endl << endl;\n" + "//\n" + "// system(\"pause\");\n" + "//}\n\n\n" + "//\n" + "//// Bai 3\n" + "//#include <iostream>\n" + "//#include <string>\n" + "//\n" + "//using namespace std;\n" + "//\n" + "//string s;\n" + "//char KQ[65];\n" + "//int n;\n" + "//\n" + "//\n" + "//int check_Char(string s) {\n" + "// int i, len;\n" + "// len = s.length();\n" + "// for (i = 0; i < len; i++) {\n" + "// if (s[i] - 48 < 0 || s[i] - 48 > 9)\n" + "// return 0;\n" + "// }\n" + "//\n" + "// return 1;\n" + "//}\n" + "//\n" + "//void init() {\n" + "// int len;\n" + "// cout << \"Khoi tao: \" << endl;\n" + "// \n" + "// do {\n" + "// cout << \" - Nhap vao chuoi S: \";\n" + "// getline(cin, s);\n" + "// len = s.length();\n" + "// } while (1 > len || len > 20 || !check_Char(s));\n" + "//\n" + "//}\n" + "//\n" + "//unsigned long long convertToLong(string s) {\n" + "// unsigned long long number = 0;\n" + "// int i, len;\n" + "//\n" + "// len = s.length();\n" + "//\n" + "// for (i = 0; i < len; i++) {\n" + "// number = number * 10 + s[i] - 48;\n" + "// }\n" + "//\n" + "// return number;\n" + "//}\n" + "//\n" + "//void convertToBinary(unsigned long long a) {\n" + "// int i;\n" + "// i = 0;\n" + "// while (a) {\n" + "// KQ[i] = a % 2 + 48;\n" + "// a = a / 2;\n" + "// i++;\n" + "// }\n" + "//\n" + "// n = i;\n" + "//}\n" + "//\n" + "//void valueMax() {\n" + "// int i, flag, j;\n" + "// char* output = (char*)calloc(n, sizeof(char));\n" + "//\n" + "// flag = 0;\n" + "// j = 0;\n" + "// for (i = n - 1; i >= 0; i--) {\n" + "// if (KQ[i] != '0') {\n" + "// output[j] = KQ[i];\n" + "// j++;\n" + "// }\n" + "// else {\n" + "// if (flag == 0 && KQ[i] == '0')\n" + "// flag = 1;\n" + "// else {\n" + "// output[j] = KQ[i];\n" + "// j++;\n" + "// }\n" + "// }\n" + "// }\n" + "//\n" + "// // In KQ\n" + "// for (i = 0; i < j; i++)\n" + "// cout << output[i];\n" + "//\n" + "//\n" + "//}\n" + "//\n" + "//int main() {\n" + "// int i;\n" + "// unsigned long long value;\n" + "// char* binary = (char*)calloc(n, sizeof(char));\n" + "//\n" + "// // Cau 1\n" + "// cout << endl << \"Cau 1: \" << endl;\n" + "// init();\n" + "//\n" + "// // Cau 2\n" + "// cout << endl << \"Cau 2: \" << endl;\n" + "// cout << \" \" << s << \" = \";\n" + "// value = convertToLong(s);\n" + "// convertToBinary(value);\n" + "// for (i = n - 1; i >= 0; i--)\n" + "// cout << KQ[i];\n" + "//\n" + "// // Cau 3\n" + "// cout << endl << \"Cau 3: \" << endl;\n" + "// cout << \" - Gia tri cua S: \" << s << endl;\n" + "// cout << \" - Gia tri cua Sb la: \";\n" + "// for (i = n - 1; i >= 0; i--)\n" + "// cout << KQ[i];\n" + "//\n" + "// cout << endl << \" - Gia tri cua Sb' la: \";\n" + "// valueMax();\n" + "//\n" + "// cout << endl << endl;\n" + "//\n" + "// system(\"pause\");\n" + "//}\n\n\n\n\n" + "// Bai 1\n" + "#include <iostream>\n\n" + "using namespace std;\n\n" + "int a, b;\n\n" + "void init() {\n" + " cout << \"Khoi tao: \" << endl;\n" + " cout << \" - Nhap gia tri cua a: \";\n" + " cin >> a;\n" + " cout << \" - Nhap gia tri cua b: \";\n" + " cin >> b;\n" + "}\n\n" + "double TBC() {\n" + " double KQ;\n" + " KQ = (a + b)*1.0 / 2;\n\n" + " return KQ;\n" + "}\n\n" + "int abs(int a) {\n" + " if (a > 0)\n" + " return a;\n" + " return 0 - a;\n" + "}\n\n\n" + "int main() {\n" + " init();\n" + " double KQ;\n\n" + " // Cau 1\n" + " KQ = TBC();\n" + " cout << endl << \"Cau 1: \" << endl;\n" + " cout << \" - Trung binh cong cua \" << a << \" & \" << b << \" la: \" <<KQ;\n\n" + " // Cau 2\n" + " cout << endl << \"Cau 2: \" << endl;\n" + " if (abs(a) > abs(b))\n" + " cout << \"a^2 > b^2\" << endl;\n" + " else if (abs(a) < abs(b))\n" + " cout << \"a^2 < b^2\" << endl;\n" + " else\n" + " cout << \"a^2 = b^2\" << endl;\n\n\n" + " system(\"pause\");\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