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

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"back up code" test_str = ("//9-digits phone number\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " int n;\n" " string s;\n" " cin >> n;\n" " getline(cin,s);\n" " while(n--){\n" " regex phone10(\"09[0-9]{2}(\\\\.[0-9]{3}){2}\");\n" " string st;\n" " getline(cin, st);\n" " if(regex_match(st, phone10))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n" "//11 digits phone number\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex phone10(\"01[0-9]{9}\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, phone10))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n" "//alphabet string\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex pattern(\"[a-zA-Z\\\\s]+\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, pattern))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n" "//Alpha-numberic Strings With Limited Length\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex pattern(\"[a-z0-9]{6,20}\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, pattern))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n" "//bound checking for a Product\n" "#include <stdio.h>\n" "#include <stdlib.h>\n" "#include <limits.h>\n\n" "int main()\n" "{\n" " unsigned int a,b;\n" " scanf(\"%u%u\",&a,&b);\n" " if(a!=0 && b!=0 && UINT_MAX/a<b)\n" " printf(\"No\");\n" " else\n" " printf(\"Yes\");\n" "}\n\n\n\n" "//bound checking for a Signed Product\n" "#include <stdio.h>\n" "#include <stdlib.h>\n" "#include <limits.h>\n\n" "int main()\n" "{\n" " int a,b;\n" " scanf(\"%d%d\",&a,&b);\n" " if(a>0&&b>0){\n" " if(INT_MAX/a<b)\n" " printf(\"No\");\n" " else\n" " printf(\"Yes\");\n" " }\n" " else if(a<0&&b<0){\n" " if(INT_MAX/b>a)\n" " printf(\"No\");\n" " else\n" " printf(\"Yes\");\n" " }\n" " else if(a>0&&b<0){\n" " if(INT_MIN/a>b)\n" " printf(\"No\");\n" " else\n" " printf(\"Yes\");\n" " }\n" " else if(a<0&&b>0){\n" " if(INT_MIN/b>a)\n" " printf(\"No\");\n" " else\n" " printf(\"Yes\");\n" " }\n" " else\n" " printf(\"Yes\");\n\n" "}\n\n\n\n" "//bound checking for a Signed sum\n" "#include <stdio.h>\n" "#include <stdlib.h>\n" "#include <limits.h>\n\n" "int main()\n" "{\n" " int a,b;\n" " scanf(\"%d%d\",&a,&b);\n" " if(a>0 && b>0 && b>INT_MAX-a)\n" " printf(\"No\");\n" " else if(a<0 && b<0 && b<INT_MIN-a)\n" " printf(\"No\");\n" " else\n" " printf(\"Yes\");\n" "}\n\n\n\n" "//Bound checking for a Sum\n" "#include <stdio.h>\n" "#include <stdlib.h>\n" "#include <limits.h>\n\n" "int main()\n" "{\n" " unsigned int a,b;\n" " scanf(\"%u%u\",&a,&b);\n" " if(UINT_MAX-a>=b)\n" " printf(\"Yes\");\n" " else\n" " printf(\"No\");\n" "}\n\n\n" "//bound checking for an Integer\n" "#include <stdio.h>\n" "#include <stdlib.h>\n" "#include <string.h>\n\n" "int main()\n" "{\n" " char *s1 = (char*)malloc(12*sizeof(char));\n" " char *s2 = (char*)malloc(12*sizeof(char));\n" " gets(s1);\n" " int a = atoi(s1);\n" " sprintf(s2,\"%d\",a);\n" " if(strcmp(s1,s2)==0)\n" " printf(\"Yes\");\n" " else\n" " printf(\"No\");\n\n" "}\n\n" "//bound checking for the LCM\n" "#include <stdio.h>\n" "#include <stdlib.h>\n" "#include <limits.h>\n\n" "unsigned long long gcd(unsigned long long a, unsigned long long b) {\n" " unsigned long long temp = 0;\n" " while (b != 0)\n" " {\n" " temp = a % b;\n" " a = b;\n" " b = temp;\n" " }\n" " return a;\n" "}\n\n" "int main(void) {\n" " unsigned long long a, b;\n" " scanf(\"%llu %llu\", &a, &b);\n\n" " unsigned long long UCLN = gcd(a,b);\n" " if(a==0||b==0)\n" " printf(\"N/A\");\n" " else if((a/UCLN) <= (ULLONG_MAX/b))\n" " printf(\"%llu\",a/UCLN*b);\n" " else{\n" " printf(\"N/A\");\n" " }\n\n" " return 0;\n" "}\n\n\n\n\n" "//dual format telephone number\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex phone10(\"(\\\\+84|0)1[0-9]{9}\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, phone10))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n\n" "//email address\n" "#include <iostream>\n" "#include <regex>\n" "#include <string>\n" "using namespace std;\n\n" "int main(){\n" " regex pattern(\"([\\\\w!#$%&'*+/=?^_`{|}~-]+\\\\.?[\\\\w!#$%&'*+/=?^_`{|}~-]+)+@(\\\\w+(\\\\-\\\\w+)?(\\\\.\\\\w+))+\");\n" " int x;\n" " cin >>x;\n" " string line_;\n" " getline(cin,line_);\n" " while(x--){\n" " getline(cin,line_);\n" " if(regex_match(line_,pattern))\n" " cout << \"Valid!\"<<endl;\n" " else\n" " cout <<\"Invalid!\"<<endl;\n" " }\n" " return 0;\n" "}\n\n\n\n" "//find IP attack\n" "#include <iostream>\n" "#include <regex>\n" "#include <string>\n" "using namespace std;\n\n" "int main(){\n" " regex pattern(\"\\\\d+\\\\ \\\\d+\\\\.\\\\d+(\\\\ ((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])){2}\\\\ [A-Z]+\\\\ \\\\d+ [A-Z]+\\\\ [\\\\w-+/?=&.: ]+\");\n" " int t;\n" " string st;\n" " cin >> t;\n" " getline(cin, st);\n" " int i = 1;\n" " while(t--){\n" " getline(cin, st);\n" " cout << i++ << endl;\n" " sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);\n" " sregex_token_iterator end;\n" " for (; pos!=end; ++pos)\n" " cout << \"Found \" << pos->str() << endl;\n" " }\n" " return 0;\n" "}\n\n\n\n" "//integer number\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex pattern(\"[\\\\+-]?[1-9][0-9]*\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, pattern))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n\n" "//list email in raw data\n" "#include <iostream>\n" "#include <regex>\n" "#include <string>\n" "using namespace std;\n\n" "int main(){\n" " regex pattern(\"([\\\\w_-]+(\\\\.?[\\\\w_-]))+@[\\\\w]+([_\\\\w]+)?(\\\\.[\\\\w]+)+\");\n" " int t;\n" " string st;\n" " cin >> t;\n" " getline(cin, st);\n" " int i = 1;\n" " while(t--){\n" " getline(cin, st);\n" " cout << i++ << endl;\n" " sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);\n" " sregex_token_iterator end;\n" " for (; pos!=end; ++pos)\n" " cout << \"Found \" << pos->str()<< endl;\n" " }\n" " return 0;\n" "}\n\n\n\n\n" "//list IP in log web\n" "#include <iostream>\n" "#include <regex>\n" "#include <string>\n" "using namespace std;\n\n" "int main(){\n" " regex pattern(\"((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\");\n" " int t;\n" " string st;\n" " cin >> t;\n" " getline(cin, st);\n" " int i = 1;\n" " while(t--){\n" " getline(cin, st);\n" " cout << i++ << endl;\n" " sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);\n" " sregex_token_iterator end;\n" " for (; pos!=end; ++pos)\n" " cout << \"Found \" << pos->str()<< endl;\n" " }\n" " return 0;\n" "}\n\n\n\n\n" "//list link in a tag HTML\n" "#include <iostream>\n" "#include <regex>\n" "#include <string>\n" "using namespace std;\n\n" "int main(){\n" " regex pattern(\"(http|https|ftp)://[\\\\w\\\\.]+([-\\\\w\\\\.]+)?(:\\\\d+)?([/\\\\w]+)?\");\n" " int t;\n" " string st;\n" " cin >> t;\n" " getline(cin, st);\n" " int i = 1;\n" " while(t--){\n" " getline(cin, st);\n" " cout << i++ << endl;\n" " sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);\n" " sregex_token_iterator end;\n" " for ( ; pos!=end ; ++pos )\n" " cout << \"Found: \" << pos->str()<< endl;\n" " }\n" " return 0;\n" "}\n\n\n\n" "//list phone number\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex pattern(\"((\\\\+84)|0)[1-9][0-9]{8,9}\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin, st);\n" " for(int i=1;i<=n;i++){\n" " cout << i << endl;\n" " getline(cin, st);\n" " sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);\n" " sregex_token_iterator end;\n" " for ( ; pos!=end ; ++pos ) {\n" " cout << \"Found \" << pos->str() << \" at \" << pos->first-st.begin() << endl;\n" " }\n" " } \n" " return 0;\n" "}\n\n\n\n" "//matching specific FileName\n" "#include <iostream>\n" "#include <regex>\n" "#include <string>\n" "using namespace std;\n\n" "int main(){\n" " regex images(\"[a-zA-Z0-9\\\\_\\\\.]+\\\\.?[a-zA-Z0-9\\\\_]+\\\\.(([jJ][pP][gG])|([pP][nN][gG])|([gG][iI][fF]))\");\n" " regex docs(\"[a-zA-Z0-9\\\\_\\\\.]+\\\\.?[a-zA-Z0-9\\\\_]+\\\\.(([pP][dD][f|])|([dD][oO][cC])|([pP]{2}[tT][xX]))\");\n" " regex media(\"[a-zA-Z0-9\\\\_\\\\.]+\\\\.?[a-zA-Z0-9\\\\_]+\\\\.(([m|M][p|P][3|4])|([a|A][v|V][i|I]))\");\n" " int t;\n" " string st;\n" " cin >> t;\n" " getline(cin, st);\n" " int i = 1;\n" " while(t--){\n" " getline(cin, st);\n" " cout << i++ << endl;\n" " \n" " sregex_token_iterator pos(st.cbegin(),st.cend(), images, 0);\n" " sregex_token_iterator end;\n" " for ( ; pos!=end ; ++pos )\n" " cout << pos->str() << \" : \" << \"hinh anh\" << endl;\n" " \n" " sregex_token_iterator pos1(st.cbegin(),st.cend(), media, 0);\n" " sregex_token_iterator end1;\n" " for ( ; pos1!=end1 ; ++pos1 )\n" " cout << pos1->str() << \" : \" << \"media\" << endl;\n" " \n" " sregex_token_iterator pos2(st.cbegin(),st.cend(), docs, 0);\n" " sregex_token_iterator end2;\n" " for ( ; pos2!=end2 ; ++pos2 )\n" " cout << pos2->str() << \" : \" << \"tai lieu\" << endl;\n" " }\n" " return 0;\n" "}\n\n\n\n\n" "//modify telephone number\n" "#include <iostream>\n" "#include <regex>\n" "#include <string>\n" "using namespace std;\n\n" "string Convert(string str){\n" " string temp = \"\";\n" " string sub = str.substr(0,4);\n" " if(sub.compare(\"0123\") ==0){\n" " regex at_sign(\"0123\");\n" " string replace_by(\"083\");\n" " temp = regex_replace(str, at_sign, replace_by);\n" " }else if(sub.compare(\"0124\") ==0){\n" " regex at_sign(\"0124\");\n" " string replace_by(\"084\");\n" " temp = regex_replace(str, at_sign, replace_by);\n" " }else if(sub.compare(\"0125\") ==0){\n" " regex at_sign(\"0125\");\n" " string replace_by(\"085\");\n" " temp = regex_replace(str, at_sign, replace_by);\n" " }\n" " else if(sub.compare(\"0127\") ==0){\n" " regex at_sign(\"0127\");\n" " string replace_by(\"081\");\n" " temp = regex_replace(str, at_sign, replace_by);\n" " }\n" " else if(sub.compare(\"0129\") ==0){\n" " regex at_sign(\"0129\");\n" " string replace_by(\"082\");\n" " temp = regex_replace(str, at_sign, replace_by);\n" " }\n" " return temp;\n" "}\n" "int main(){\n" " regex pattern(\"012[3-9^6]\\\\d{7}\");\n" " int t;\n" " string st;\n" " cin >> t;\n" " getline(cin, st);\n\n" " for(int i = 1; i <= t ; i++){\n" " getline(cin, st);\n" " cout << i << endl;\n" " sregex_token_iterator pos(st.cbegin(),st.cend(), pattern, 0);\n" " sregex_token_iterator end;\n" " for (; pos!=end; ++pos){\n" " string str1 = pos->str();\n" " cout << \"Thay doi: \" << Convert(str1) << endl;\n" " }\n" " }\n" " return 0;\n" "}\n\n\n\n" "//numeric string leading without zero\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex pattern(\"[1-9][0-9]*\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, pattern))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n\n" "//numeric string\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex pattern(\"[0-9]*\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, pattern))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n\n" "//telephone number international format\n" "#include<iostream>\n" "#include<regex>\n" "#include <string>\n" "#include <stdexcept>\n" "using namespace std;\n" "int main()\n" "{\n" " regex phone10(\"\\\\+841(\\\\.[0-9]{3}){3}\");\n" " string st;\n" " int n;\n" " cin >> n;\n" " getline(cin,st);\n" " while(n--){\n" " getline(cin, st);\n" " if(regex_match(st, phone10))\n" " cout << \"Valid!\" << endl;\n" " else\n" " cout << \"Invalid!\" << endl;\n" " }\n" " \n" " return 0;\n" "}\n\n\n\n") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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