Regular Expressions 101

Save & Share

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 re regex = re.compile(r"int_err", flags=re.MULTILINE) test_str = ("v#include <iostream>\n" "#include <string>\n" "#include <climits>\n" "#include <cstring>\n" "#include <cstdlib>\n" "using namespace std;\n\n" "int BitCount(uint64_t);\n" "bool Addition_Is_Safe_Bits(uint64_t, uint64_t, const int);\n" "bool short_Addition_Is_Safe_Value(short, short);\n" "bool Multiplication_Is_Safe_Bits(uint64_t, uint64_t, const int);\n" "bool int_Multiplication_Is_Safe_Value(uint32_t, uint32_t);\n" "bool shortValidation(string);\n" "void addAnCharacter(char *, int, char);\n" "char* DecimalToBinary(unsigned int);\n" "char* AxorB(char *, char *);\n" "int BinaryToDecimal(char *s);\n" "unsigned int myPow(unsigned int, unsigned int, bool &);\n\n" "int main()\n" "{\n\n" "#if 0 // Program 2\n" " string strMoney, strRate;\n\n" "lb_input:\n" " do\n" " {\n" " // Dữ liệu nhập quy hết về chuỗi sau đó chuỗi được Validation\n" " cout << \"Money: \";\n" " getline(cin, strMoney);\n" " cout << \"Rate (e.g: for 10% enter 10): \";\n" " getline(cin, strRate);\n" " } while (!shortValidation(strMoney) || !shortValidation(strRate));\n" " if (atoi((char *)strMoney.c_str()) > SHRT_MAX || atoi((char *)strRate.c_str()) > 100)\n" " goto lb_input;\n\n" " short money = static_cast<short>(atoi((char *)strMoney.c_str()));\n" " short rate = static_cast<short>(atoi((char *)strRate.c_str()));\n\n" " float InterestRate = static_cast<float>(rate) / 100;\n" " cout << \"Interest Rate: \" << InterestRate << \" %\" << endl;\n" " cout << \"---------------------------\" << endl;\n\n" " cout << \"Year\\t\\tGrowth\\t\\tNew balance\" << endl;\n\n" " bool flag = true; // Đặt cờ phát hiện tràn số để từ đó đưa ra thông báo tràn số\n" " for (int i = 1; i <= 10; i++)\n" " {\n" " short increase = static_cast<short>(money * InterestRate);\n" " short newmoney = money + increase;\n\n" " if (!short_Addition_Is_Safe_Value(money, increase)) // Cách 2: if(!Addition_Is_Safe_Bits(money, increase, 15))\n" " {\n" " flag = false;\n" " break;\n" " }\n\n" " cout << i << \"\\t\\t\" << increase << \"\\t\\t\" << newmoney << endl;\n" " money = newmoney;\n" " }\n\n" " if (!flag)\n" " {\n" " cout << \"Warning: Integer Error...!!\" << endl;\n" " goto lb_input; // Nếu tràn số thì nhập lại và tính tiếp (Không bắt buộc, tùy để bài, có thể tràn số chỉ cần in ra thông báo mà không bắt nhập lại)\n" " }\n\n" " cout << \"Total: \" << money << endl;\n\n" "#endif // 1 // Program 2\n\n" "#if 1 // Program 3\n" " // unsigned int: 0 -> 4.294.967.295\n" " unsigned int x, y;\n\n" " // Input\n" " cout << \"x = \"; cin >> x;\n" " cout << \"y = \"; cin >> y;\n" " cout << \"------------------------\" << endl;\n\n" " // Decimal To Binary\n" " char *bin_x1 = DecimalToBinary(x);\n" " char *bin_y1 = DecimalToBinary(y);\n\n" " cout << \"Decimal To Binary\" << endl;\n" " cout << x << \"\\t:\\t\" << bin_x1 << endl;\n" " cout << y << \"\\t:\\t\" << bin_y1 << endl;\n" " cout << \"------------------------\" << endl;\n\n" " // XOR\n" " /*char *bin_x2 = DecimalToBinary(x);\n" " char *bin_y2 = DecimalToBinary(y);*/\n" " char * xorStr = AxorB(bin_x1, bin_y1);\n\n" " cout << \"X xor Y\" << endl;\n" " cout << \" x\\t\\t\" << x << \"\\t\" << bin_x1 << endl;\n" " cout << \" y\\t\\t\" << y << \"\\t\" << bin_y1 << endl;\n" " cout << \"x xor y\\t\\t\" << BinaryToDecimal(xorStr) << \"\\t\" << xorStr << endl;\n" " cout << \"------------------------\" << endl;\n\n" " // POW\n" " bool flag = true;\n" " cout << \"POW(X, Y)\" << endl;\n" " cout << \" x\\t = \\t\" << x << endl;\n" " cout << \" y\\t = \\t\" << y << endl;\n" " unsigned int z = myPow(x, y, flag);\n\n" " if (!flag)\n" " cout << \"Warning: Integer Error..!!\" << endl;\n" " cout << \"------------------------\" << endl;\n\n" " // free / delete\n" " free(bin_x1);\n" " free(bin_y1);\n" "#endif // 1 // Program 3\n\n" " system(\"pause\");\n" " return 0;\n" "}\n\n" "bool shortValidation(string str)\n" "{\n" " // Hàm Validation một string xem nó có hợp lệ hay không\n" " // unsigned short: 0..65535\n" " if (str.length() > 5 || str.length() == 0)\n" " return false;\n\n" " for (int i = 0; i < str.length(); i++)\n" " if (str.at(i) < '0' || str.at(i) > '9')\n" " return false;\n\n" " // Số nhập vào phải ít nhất là 1 (Không bắt buộc nếu đề bài không yêu cầu)\n" " double num = atof((char *)str.c_str());\n" " if (num < 1)\n" " return false;\n\n" " return true;\n" "}\n" "int BitCount(uint64_t a)\n" "{\n" " // Hàm đếm số bit của một số nguyên\n" " int bits = 0;\n" " while (a != 0)\n" " {\n" " ++bits;\n" " a >>= 1; // Dịch phải một bit. Dịch đến khi a = 0 thì sẽ kết thúc vòng lặp\n" " };\n\n" " return bits;\n" "}\n" "bool Addition_Is_Safe_Bits(uint64_t a, uint64_t b, const int nBit)\n" "{\n" " // Hàm kiểm tra phép cộng hai số nguyên xem liệu có bị tràn hay không\n" " uint64_t x = a + b;\n" " int x_bits = BitCount(x); // Đếm xem x biểu diễn bởi bao nhiêu bit\n" " return (x_bits <= nBit);\n" "}\n" "bool short_Addition_Is_Safe_Value(short a, short b)\n" "{\n" " // Tương tự hàm Addition_Is_Safe_Bits nhưng hàm này không kiểm tra theo số bit mà kiểm tra theo giá trị (value)\n\n" " /*\n" " SHRT_MAX = 32767, SHRT_MIN = -32768\n\n" " - Giả sử: a = 32000\n" " - Cho phép thực hiện cộng khi: b <= 767 (tức là: 32767 - a)\n\n" " - Giả sử: a = -32000\n" " - Cho phép thực hiện cộng khi: b >= -768 (tức là: -32768 - (a) )\n" " */\n\n" " // Tổng quát với kiểu short: -32768..32767\n" " if ((a > 0) && (b > SHRT_MAX - a))\n" " return false; // Tràn trên\n" " if ((a < 0) && (b < SHRT_MIN - a))\n" " return false; // Tràn dưới\n\n" " return true;\n" "}\n" "void addAnCharacter(char *s, int vt, char gt)\n" "{\n" " // Hàm thêm một ký tự vào một vị trí bất kỳ trong chuỗi s\n" " int n = strlen(s);\n" " for (int i = n - 1; i >= vt; i--)\n" " s[i + 1] = s[i];\n" " s[vt] = gt;\n" " s[n + 1] = '\\0';\n" "}\n" "char* DecimalToBinary(unsigned int x)\n" "{\n" " // Hàm chuyển đổi một số nguyên dương hệ 10 sang hệ 2\n" " char s[45];\n" " int i = 0;\n" " while (x != 0)\n" " {\n" " unsigned int tmp = x % 2;\n" " tmp += 48;\n" " s[i] = tmp;\n" " x /= 2;\n" " i++;\n" " }\n" " s[i] = '\\0';\n" " // Mảng ký tự s[i] là một chuỗi các ký tự '0' và '1'. Để hiển thị đúng dạng nhị phân của số cần đọc theo thứ tự ngược từ cuối mảng về đầu mảng\n\n" " // Cấp phát động một mảng ký tự là dạng nhị phân của số thập phân. Hàm sẽ trả về mảng ký tự này\n" " int index = 0, len = i;\n" " char *str = (char *)malloc((len + 1) * sizeof(char));\n\n" " for (int j = i - 1; j >= 0; j--)\n" " str[index++] = s[j];\n" " str[index] = '\\0';\n\n" " return str;\n" "}\n" "char* AxorB(char *a, char *b)\n" "{\n" " /*\n" " Phép XOR thực hiện trên hai dãy bit cùng độ dài\n" " A B A^B\n" " 0 0 0\n" " 0 1 1\n" " 1 0 1\n" " 1 1 0\n" " Chuẩn hóa hai chuỗi cùng độ dài trước khi XOR\n" " VD:\n" " a = 10010110 => 8 ký tự\n" " b = 10110 => 5 ký tự\n" " => Cần phải thêm 3 ký tự vào chuỗi b\n" " */\n\n" " int na = strlen(a);\n" " int nb = strlen(b);\n\n" " // Chuẩn hóa hai chuỗi\n" " if (na > nb)\n" " for (int i = 0; i < na - nb; i++)\n" " addAnCharacter(b, 0, '0');\n" " if (nb > na)\n" " for (int i = 0; i < nb - na; i++)\n" " addAnCharacter(a, 0, '0');\n\n" " // XOR\n" " int sizeXorStr = strlen(a) + 1;\n" " char *xorStr = (char *)malloc(sizeXorStr * sizeof(char));\n" " for (int i = 0; i < strlen(a); i++)\n" " {\n" " if (a[i] == b[i])\n" " xorStr[i] = '0';\n" " else\n" " xorStr[i] = '1';\n" " }\n" " xorStr[strlen(a)] = '\\0';\n\n" " return xorStr;\n" "}\n" "int BinaryToDecimal(char *s)\n" "{\n" " // Hàm chuyển đổi một chuỗi nhị phân sang một số nguyên\n" " int n = 0; // Giá trị thập phân sẽ trả về\n" " int length = strlen(s);\n" " int somu = length - 1;\n\n" " for (int i = 0; i < length; i++)\n" " n += (s[i] - 48) * pow((double)2, somu--);\n\n" " return n;\n" "}\n" "unsigned int myPow(unsigned int x, unsigned int y, bool &flag)\n" "{\n" " // Hàm tính lỹ thữa x mũ y\n\n" " // Các trường hợp đặc biệt\n" " if (x == 0)\n" " {\n" " if (y != 0) return 0;\n" " else return 1;\n" " }\n" " if (y == 0) return 1;\n\n" " // POW\n" " uint64_t sum = 1;\n" " for (int i = 1; i <= y; i++)\n" " {\n" " if (!int_Multiplication_Is_Safe_Value(sum, x))//Cách 2: if (!Multiplication_Is_Safe_Bits(sum, x, 32)) \n" " {\n" " flag = false;\n" " break;\n" " }\n" " sum *= x;\n" " cout << x << \" ^ \" << i << \"\\t =\\t\" << sum << endl;\n" " }\n\n" " return sum;\n" "}\n" "bool Multiplication_Is_Safe_Bits(uint64_t a, uint64_t b, const int nBit)\n" "{\n" " // Hàm kiểm tra xem phép nhân hai số nguyên liệu có bị tràn số hay không\n" " uint64_t x = a * b;\n" " size_t x_bits = BitCount(x);\n" " return (x_bits <= nBit);\n" "}\n" "bool int_Multiplication_Is_Safe_Value(uint32_t a, uint32_t b)\n" "{\n" " // Tương tự như hàm Multiplication_Is_Safe_Bits nhưng hàm này kiểm tra theo giá trị (value)\n\n" " /*\n" " UINT32_MAX = 4294967295, UINT32_MIN = 0\n\n" " VD: MAX = 100\n" " - Giả sử: a = 25\n" " - Cho phép thực hiện phép nhân khi: b <= 4 (tức là: MAX / a)\n" " */\n\n" " if (a > 0 && b > UINT32_MAX / a)\n" " return false;\n\n" " return true;\n" "}\n\n" "// Ref: https://stackoverflow.com/questions/199333/how-to-detect-integer-overflow\n" "/*\n" "** NOTE:\n" "- Đoạn mã trong cặp if-endif:\n" " #if 0\n" " // Code here\n" " #endif\n\n" " Trong đó:\n" " + Với #if 0 hiểu là comment đoạn code đó lại\n" " + Với #if 1 hiểu là bỏ comment đoạn code đó\n" "*/\n") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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 Python, please visit: https://docs.python.org/3/library/re.html