import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "[a-z0-9 %\\-\\+:\\/]*\\b(\\d{1,3}\\.?\\d{0,4})\\.?[a-z0-9 \\(\\)]*out of[a-z ]*(\\d{1,3}\\.?\\d{0,4})\\.?.*";
final String string = "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";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html