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

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

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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm