# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"l4.2"
test_str = ("// Lab 4.2\n"
"#include <iostream>\n"
"#include <string>\n\n"
"using namespace std;\n\n"
"string s1, s2;\n\n"
"int checkChar() {\n"
" int i, len;\n\n"
" len = s2.length();\n"
" for (i = 0; i < len; i++)\n"
" if (s2[i] - 48 < 0 || s2[i] - 48 > 9)\n"
" return 0;\n\n"
" return 1;\n"
"}\n\n"
"void inputString() {\n"
" cout << \" - Nhap chuoi S1: \";\n"
" getline(cin, s1);\n"
" \n"
" while (1) {\n"
" cout << \" - Nhap chuoi S2: \";\n"
" getline(cin, s2);\n\n"
" if (!checkChar())\n"
" cout << \" => \" << \"Input Validation\" << endl << endl;\n"
" else\n"
" break;\n"
" }\n"
"}\n\n"
"int checkNumber() {\n"
" int i, len;\n"
" len = s1.length();\n\n"
" for (i = 0; i < len; i++)\n"
" if (s1[i] - 48 >= 0 && s1[i] - 48 <= 9)\n"
" return 1;\n\n"
" return 0;\n"
"}\n\n"
"int checkSDTMobifone() {\n"
" int i, len;\n"
" len = s2.length();\n\n"
" if (len==10 && s2[0] - 48 == 0 && s2[1] - 48 == 9 && s2[2] - 48 == 0) {\n"
" for (i = 3; i < 10; i++) {\n"
" if (s2[i] - 48 < 0 || s2[i] - 48 > 9)\n"
" return 0;\n"
" }\n"
" return 1;\n"
" }\n"
" else\n"
" return 0;\n"
"}\n\n"
"int checkSubString() {\n"
" int i, j;\n"
" int len, len_s1, len_s2, flag;\n"
" char temp;\n\n"
" len_s1 = s1.length();\n"
" len_s2 = s2.length();\n\n"
" if (len_s2 == 0)\n"
" return 1;\n\n"
" if (len_s1 >= len_s2) {\n"
" for (i = 0; i <= len_s1 - len_s2; i++) {\n"
" flag = 1;\n"
" for (j = 0; flag&&j < len_s2; j++) {\n"
" if (s1[i + j] != s2[j])\n"
" flag = 0;\n"
" }\n"
" if (flag)\n"
" return 1;\n"
" }\n"
" }\n"
" \n"
" return 0;\n"
"}\n\n"
"string addString(){\n"
" string s3;\n"
" int i, len_s1, len_s2, len;\n\n"
" len_s1 = s1.length();\n"
" len_s2 = s2.length();\n"
" len = len_s1 + len_s2;\n\n"
" for (i = 0; i < len; i++) {\n"
" if (i < len_s1)\n"
" s3 += s1[i];\n"
" else\n"
" s3 += s2[i - len_s1];\n"
" }\n\n"
" return s3;\n"
"}\n\n"
"void insertString(string s3) {\n"
" string str_Insert = \"Buffer_Overflow\";\n"
" char after;\n"
" int i, len, flag;\n\n"
" len = s3.length();\n\n"
" cout << \" - Chuoi S3: \" << s3 << endl;\n\n"
" cout << \" - Ban muon them vao sau ky tu nao: \";\n"
" cin >> after;\n\n"
" cout << endl << \" => \";\n\n"
" flag = 0;\n"
" for (i = 0; i < len; i++) {\n"
" if (s3[i] != after) {\n"
" cout << s3[i];\n"
" }\n"
" else {\n"
" cout << after << str_Insert;\n"
" flag++;\n"
" }\n"
" }\n\n"
" if (!flag)\n"
" cout << str_Insert << endl;\n"
"}\n\n"
"int main() {\n\n"
" string s1, s2, s3;\n"
" // Cau 1\n"
" cout << endl << \"Cau 1: \" << endl;\n"
" inputString();\n\n"
" // Cau 2\n"
" cout << endl << \"Cau 2: \" << endl;\n"
" if (checkNumber())\n"
" cout << \" - Chuoi S1 co chu so\" << endl;\n"
" else\n"
" cout << \" - Chuoi S1 khong co chu so\" << endl;\n\n"
" // Cau 3\n"
" cout << endl << \"Cau 3: \" << endl;\n"
" if (checkSDTMobifone())\n"
" cout << \" - Chuoi S2 la SDT mang Mobifone\" << endl;\n"
" else\n"
" cout << \" - Chuoi S2 khong la SDT mang Mobifone\" << endl;\n\n"
" // Cau 4\n"
" cout << endl << \"Cau 4: \" << endl;\n"
" if (checkSubString())\n"
" cout << \" - Chuoi S2 la tap con cua chuoi S1\" << endl;\n"
" else\n"
" cout << \" - Chuoi S2 khong la tap con cua chuoi S1\" << endl;\n\n"
" // Cau 5\n"
" cout << endl << \"Cau 5: \" << endl;\n"
" cout << \" - Chuoi S3 = S1 + S2: \";\n"
" s3 = addString();\n"
" cout << s3 << endl;\n\n"
" // Cau 6\n"
" cout << endl << \"Cau 6: \" << endl;\n"
" insertString(s3);\n"
" cout << endl << endl;\n\n\n"
" //system(\"pause\");\n"
" \n"
" return 0;\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