You can simplify your regex a bit and do it this way:
Pattern FRACTION_PATTERN = Pattern.compile("(?:\\d+\\s+)?\\d/\\d");
Matcher m = FRACTION_PATTERN.matcher("1/2 + 1 1/2");
while (m.find()) {
String found = m.group();
}
It will find 1/2
and 1 1/2
.
If you want to capture only 1 1/2
, use pattern:
Pattern FRACTION_PATTERN = Pattern.compile("\\d+\\s+\\d/\\d");