Java重复模式匹配 [英] Java repetitive pattern matching
问题描述
(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ [[^ \\ [] * \\])*
匹配任何字符串包含在[]中,只要它不包含[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屋!