2

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.

4 Answers 4

5
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

Sign up to request clarification or add additional context in comments.

Comments

5
    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));
    }

1 Comment

Would you please explain your regexp ?. Thank you.
2

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]

Comments

1

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;
    }

}

1 Comment

Would you please explain your regular expression ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.