# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"[a-z0-9 %\-\+:\/]*\b(\d{1,3}\.?\d{0,4})\.?[a-z0-9 \(\)]*out of[a-z ]*(\d{1,3}\.?\d{0,4})\.?.*"
test_str = ("4 out of 6.0\n"
"2.979 out of 4 gpa\n"
"out of 5\n"
"gpa 3.712 out of 4\n"
"110 out of 110\n"
"4.94 (for second part) out of 6\n"
"9.45 points out of 10 (240 ects)\n"
"5.7 out of 7.\n"
"cgpa- 3.66 out of 4\n"
"a-/gpa :3.55 out of 4.00\n"
"4.5 out of a possible 5\n"
"a4 19 out of 22\n"
"1.15 1 being the best grade out of 5\n"
"3.64out of 6\n"
"28 out of 30. final grade 108 out of 110\n"
"2.64out of 4 average 74.02%\n"
"81.77. gpa:3.7out of 4.3\n"
"81.77gpa:3.7 out of 1.3\n"
"cumulative gpa 16.22out of 20.00\n"
"9.13 rank 20 out of 80\n"
"a+/9.65 out of 10\n"
"3.6/4 or 17.87 out of 20\n"
"87.1%/gpa 3.71 out of 5.0")
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