アスタリスクが付いたグループのコンテンツを取得するにはどうすればよいですか?
たとえば、カンマ区切りのリストを切り取りたいと思います1,2,3,4,5
。
private static final String LIST_REGEX = "^(\\d+)(,\\d+)*$";
private static final Pattern LIST_PATTERN = Pattern.compile(LIST_REGEX);
public static void main(String[] args) {
final String list = "1,2,3,4,5";
final Matcher matcher = LIST_PATTERN.matcher(list);
System.out.println(matcher.matches());
for (int i = 0, n = matcher.groupCount(); i < n; i++) {
System.out.println(i + "\t" + matcher.group(i));
}
}
そして、出力は
true
0 1,2,3,4,5
1 1
1
, 2
, 3
, ...など、すべてのエントリを取得するにはどうすればよいですか?
私は共通の解決策を探しています。これは単なる例です。次のようなリストに一致する
ような、より複雑な正規表現を想像してください^\\[(\\d+)(,\\d+)*\\]$
[1,2,3,4,5]