Java重复模式匹配 [英] Java repetitive pattern matching

查看:106
本文介绍了Java重复模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

没有现有的知识有多少这样的组存在,我找不到一种通过模式匹配器访问各个匹配组的方式,即无法获得

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

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



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



肯定有一个简单的方式在Java中这样做,我错过了



感谢任何帮助。

解决方案

 code> String string =[a] [nice] [repetitive] [pattern]; 
String regexp =\\ [([^ \\ [] *)\\];
模式模式= Pattern.compile(regexp);
Matcher matcher = pattern.matcher(string);
while(matcher.find()){
System.out.println(matcher.group(1));
}


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.

解决方案

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

这篇关于Java重复模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆