Use lookahead for Input and use find in a loop instead of matches :
Pattern pattern = Pattern.compile("Input(.*?)(?=Input|$)"); Matcher matcher = pattern.matcher(s); while (matcher.find()) { System.out.println(matcher.group(1)); }
See how it works on the Internet: ideone
But it is better to use split here:
String[] result = s.split("Input");
See how it works on the Internet: ideone
source share