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

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

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php