Java重复模式匹配
我试图获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://download .oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#find%28%29
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#find%28%29
我会使用分割
打印
I would use split
prints
这是我的尝试:)
Here's my attempt :)