I am trying to get each of the repetitive matches of a simple regular expression in Java:
(\\[[^\\[]*\\])*
which matches any string enclosed in [], as long as it does not contain the [ character. For example, it would match
[a][nice][repetitive][pattern]
There is no prior knowledge of how many such groups exist and I cannot find a way of accessing the individual matching groups via a pattern matcher, i.e. can't get
[a], [nice], [repetitive], [pattern]
(or, even better, the text without the brackets), in 4 different strings.
Using pattern.matcher() I always get the last group.
Surely there must be a simple way of doing this in Java, which I am missing?
Thanks for any help.
while (matcher.find()) {
System.out.println(matcher.group(1));
}
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#find%28%29
String string = "[a][nice][repetitive][pattern]";
String regexp = "\\[([^\\[]*)\\]";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
I would use split
String string = "[a][nice][repetitive][pattern]";
String[] words = string.substring(1, string.length()-1).split("\\]\\[");
System.out.println(Arrays.toString(words));
prints
[a, nice, repetitive, pattern]
Here's my attempt :)
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Foo {
public static void main(String[] args) {
final String text = "[a][nice][repetitive][pattern]";
System.out.println(getStrings(text)); // Prints [a, nice, repetitive, pattern]
}
private static final Pattern pattern = Pattern.compile("\\[([^\\]]+)]");
public static List<String> getStrings(final String text) {
final List<String> strings = new ArrayList<String>();
final Matcher matcher = pattern.matcher(text);
while(matcher.find()) {
strings.add(matcher.group(1));
}
return strings;
}
}