PNS*_*PNS 2 java design-patterns repeat matching
我试图在Java中获得一个简单正则表达式的每个重复匹配:
(\\[[^\\[]*\\])*
Run Code Online (Sandbox Code Playgroud)
它匹配[]中包含的任何字符串,只要它不包含[字符.例如,它会匹配
[a][nice][repetitive][pattern]
Run Code Online (Sandbox Code Playgroud)
先前没有关于存在多少这样的组的知识,我找不到通过模式匹配器访问各个匹配组的方法,即无法获得
[a], [nice], [repetitive], [pattern]
Run Code Online (Sandbox Code Playgroud)
(或者更好的是,没有括号的文本),有4种不同的字符串.
使用pattern.matcher()我总是得到最后一组.
当然,必须有一种简单的方法在Java中实现这一点,我错过了吗?
谢谢你的帮助.
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Run Code Online (Sandbox Code Playgroud)
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));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1050 次 |
最近记录: |