Java repetitive pattern matching

我怕爱的太早我们不能终老 提交于 2019-12-24 00:17:26

问题


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.


回答1:


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




回答2:


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



回答3:


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]



回答4:


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

}


来源:https://stackoverflow.com/questions/6340364/java-repetitive-pattern-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!