必要になるたびググって、そのたびに忘れている気がするので、自分用にメモしておきたいと思います。
正規表現にマッチした文字列すべてを取得したい場合、Java8以前であれば以下のようなイディオムとなります。
String s = "111 AAA 222 BBB 333";
Pattern p = Pattern.compile("[0-9]{3}");
Matcher m = p.matcher(s);
List<String> results = new ArrayList<>();
while (m.find()) {
results.add(m.group());
}
System.out.println(results); //=> [111, 222, 333]
Java9以降であればStreamを使って、シンプルに書くこともできます。
String s = "111 AAA 222 BBB 333";
Pattern p = Pattern.compile("[0-9]{3}");
Matcher m = p.matcher(s);
List<String> results = m.results().map(MatchResult::group).toList();
System.out.println(results); //=> [111, 222, 333]