import re
regex = re.compile(r"back up code", flags=re.MULTILINE)
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 = 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