Java重复模式匹配

发布于 2024-11-15 05:16:21 字数 441 浏览 3 评论 0原文

我试图获取 Java 中简单正则表达式的每个重复匹配:

(\\[[^\\[]*\\])*

它匹配 [] 中包含的任何字符串,只要它不包含 [ 字符。例如,它会匹配

[a][nice][repetitive][pattern]

没有先验知识存在多少个这样的组,并且我无法找到通过模式匹配器访问各个匹配组的方法,即无法获取

[a], [nice], [repetitive], [pattern]

(或者更好的是,没有括号的文本) ),有 4 个不同的字符串。

使用pattern.matcher()我总是得到最后一组。

当然,在 Java 中一定有一种简单的方法可以做到这一点,而我却缺少这种方法?

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖心男生 2024-11-22 05:16:21
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

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

-小熊_ 2024-11-22 05:16:21
    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));
    }
    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));
    }
苍景流年 2024-11-22 05:16:21

我会使用分割

String string = "[a][nice][repetitive][pattern]";
String[] words = string.substring(1, string.length()-1).split("\\]\\[");
System.out.println(Arrays.toString(words));

打印

[a, nice, repetitive, pattern]

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]
丘比特射中我 2024-11-22 05:16:21

这是我的尝试:)

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文