# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\[[+-]?([048]|\d*([02468][048]|[13579][26]))\]"
test_str = ("\"[+05620]\" // 5620 is divisible by 4 (valid)\n"
"\"[+05621]\" // 5621 is not divisible by 4 (invalid)\n"
"\"[-55622]\" // -55622 is not divisible by 4 (invalid)\n"
"\"[005623]\" // 5623 invalid\n"
"\"[005624]\" // 5624 valid\n"
"\"[-05628]\" // valid\n"
"\"[005632]\" // valid\n"
"\"[555636]\" // valid\n"
"\"[+05640]\" // valid\n"
"\"[005600]\" // valid\n"
"\"[8.04]\" // invalid\n"
"\"[8.00]\" // valid\n\n"
"\"[-0]\"\n"
"\"...may be [+002016] will be.\"\n\n"
"\"[+05621]\", \"[-55622]\", \n"
"\"[005623]\", \"[~24]\", \"[8.04]\", \n"
"\"No, [2014] isn't a multiple of 4...\"\n\n"
"\"[0]\"\n"
"\"[4]\"\n"
"\"[8]\"\n"
"\"[12]\"\n"
"\"[16]\"\n"
"\"[20]\"\n"
"\"[24]\"\n"
"\"[28]\"\n"
"\"[32]\"\n"
"\"[36]\"\n"
"\"[40]\"\n"
"\"[44]\"\n"
"\"[48]\"\n"
"\"[52]\"\n"
"\"[56]\"\n"
"\"[60]\"\n"
"\"[64]\"\n"
"\"[68]\"\n"
"\"[72]\"\n"
"\"[76]\"\n"
"\"[80]\"\n"
"\"[84]\"\n"
"\"[88]\"\n"
"\"[92]\"\n"
"\"[96]\"\n"
"\"[100]\"\n"
"\"[104]\"\n"
"\"[108]\"\n"
"\"[112]\"\n"
"\"[116]\"\n"
"\"[120]\"\n"
"\"[124]\"\n"
"\"[128]\"\n\n"
"\"[1]\"\n"
"\"[2]\"\n"
"\"[3]\"\n"
"\"[5]\"\n"
"\"[6]\"\n"
"\"[7]\"\n"
"\"[9]\"\n"
"\"[10]\"\n"
"\"[11]\"\n"
"\"[13]\"\n"
"\"[14]\"\n"
"\"[15]\"\n"
"\"[17]\"\n"
"\"[18]\"\n"
"\"[19]\"\n"
"\"[21]\"\n"
"\"[22]\"\n"
"\"[23]\"\n"
"\"[25]\"\n"
"\"[26]\"\n"
"\"[27]\"\n"
"\"[29]\"\n"
"\"[30]\"\n"
"\"[31]\"\n"
"\"[33]\"\n"
"\"[34]\"\n"
"\"[35]\"\n"
"\"[37]\"\n"
"\"[38]\"\n"
"\"[39]\"\n"
"\"[41]\"\n"
"\"[42]\"\n"
"\"[43]\"\n"
"\"[45]\"\n"
"\"[46]\"\n"
"\"[47]\"\n"
"\"[49]\"\n"
"\"[50]\"\n"
"\"[51]\"\n"
"\"[53]\"\n"
"\"[54]\"\n"
"\"[55]\"\n"
"\"[57]\"\n"
"\"[58]\"\n"
"\"[59]\"\n"
"\"[61]\"\n"
"\"[62]\"\n"
"\"[63]\"\n"
"\"[65]\"\n"
"\"[66]\"\n"
"\"[67]\"\n"
"\"[69]\"\n"
"\"[70]\"\n"
"\"[71]\"\n"
"\"[73]\"\n"
"\"[74]\"\n"
"\"[75]\"\n"
"\"[77]\"\n"
"\"[78]\"\n"
"\"[79]\"\n"
"\"[81]\"\n"
"\"[82]\"\n"
"\"[83]\"\n"
"\"[85]\"\n"
"\"[86]\"\n"
"\"[87]\"\n"
"\"[89]\"\n"
"\"[90]\"\n"
"\"[91]\"\n"
"\"[93]\"\n"
"\"[94]\"\n"
"\"[95]\"\n"
"\"[97]\"\n"
"\"[98]\"\n"
"\"[99]\"\n"
"\"[101]\"\n"
"\"[102]\"\n"
"\"[103]\"\n"
"\"[105]\"\n"
"\"[106]\"\n"
"\"[107]\"\n"
"\"[109]\"\n"
"\"[110]\"\n"
"\"[111]\"\n"
"\"[113]\"\n"
"\"[114]\"\n"
"\"[115]\"\n"
"\"[117]\"\n"
"\"[118]\"\n"
"\"[119]\"\n"
"\"[121]\"\n"
"\"[122]\"\n"
"\"[123]\"\n"
"\"[125]\"\n"
"\"[126]\"\n"
"\"[127]\"\n"
"\"[129]\"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
"0\n"
"4\n"
"8\n"
"12\n"
"16\n"
"20\n"
"24\n"
"28\n"
"32\n"
"36\n"
"40\n"
"44\n"
"48\n"
"52\n"
"56\n"
"60\n"
"64\n"
"68\n"
"72\n"
"76\n"
"80\n"
"84\n"
"88\n"
"92\n"
"96\n"
"100\n"
"104\n"
"108\n"
"112\n"
"116\n"
"120\n"
"124\n"
"128\n\n"
"1\n"
"2\n"
"3\n"
"5\n"
"6\n"
"7\n"
"9\n"
"10\n"
"11\n"
"13\n"
"14\n"
"15\n"
"17\n"
"18\n"
"19\n"
"21\n"
"22\n"
"23\n"
"25\n"
"26\n"
"27\n"
"29\n"
"30\n"
"31\n"
"33\n"
"34\n"
"35\n"
"37\n"
"38\n"
"39\n"
"41\n"
"42\n"
"43\n"
"45\n"
"46\n"
"47\n"
"49\n"
"50\n"
"51\n"
"53\n"
"54\n"
"55\n"
"57\n"
"58\n"
"59\n"
"61\n"
"62\n"
"63\n"
"65\n"
"66\n"
"67\n"
"69\n"
"70\n"
"71\n"
"73\n"
"74\n"
"75\n"
"77\n"
"78\n"
"79\n"
"81\n"
"82\n"
"83\n"
"85\n"
"86\n"
"87\n"
"89\n"
"90\n"
"91\n"
"93\n"
"94\n"
"95\n"
"97\n"
"98\n"
"99\n"
"101\n"
"102\n"
"103\n"
"105\n"
"106\n"
"107\n"
"109\n"
"110\n"
"111\n"
"113\n"
"114\n"
"115\n"
"117\n"
"118\n"
"119\n"
"121\n"
"122\n"
"123\n"
"125\n"
"126\n"
"127\n"
"129")
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