Java 正则表达式查找所有匹配项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32830013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to find all matches
提问by Ranjith
I need a regex to find all matches for my pattern.
我需要一个正则表达式来查找我的模式的所有匹配项。
The text is something like this:
文本是这样的:
"someother text !style_delete [company code : 43ev4] between text !style_delete [organiztion : 0asj9] end of line text"
And I would like to find all matches for the pattern:
我想找到该模式的所有匹配项:
!style_delete [.*]
I have tried like this:
我试过这样:
Pattern pattern = Pattern.compile("!style_delete\s*\[.*\]");
With this the match text is coming like this:
有了这个匹配文本是这样的:
!style_delete [company code : 43ev4] between text !style_delete [organiztion : 0asj9]
But I am expected as follows:
但我的预期如下:
match 1 : !style_delete [company code : 43ev4]
match 2 : !style_delete [organiztion : 0asj9]
Please help me, what will the regex in java to get above output.
请帮助我,java 中的正则表达式将如何获得以上输出。
采纳答案by StefanHeimberg
@Test
public void test() {
final String input = "someother text !style_delete [company code : 43ev4] between text !style_delete [organiztion : 0asj9] end of line text";
// my regexp:strong text
// final String regex = "(!style_delete\s\[[a-zA-Z0-9\s:]*\])";
// regexp from Trinmon:
final String regex = "(!style_delete\s*\[[^\]]*\])";
final Matcher m = Pattern.compile(regex).matcher(input);
final List<String> matches = new ArrayList<>();
while (m.find()) {
matches.add(m.group(0));
}
assertEquals(2, matches.size());
assertEquals("match 1: ", matches.get(0), "!style_delete [company code : 43ev4]");
assertEquals("match 2: ", matches.get(1), "!style_delete [organiztion : 0asj9]");
}
edit
编辑
perhaps the pattern from Trinimon's answer is a little bit more elegant. i updated the regex with the regex of him.
也许 Trinimon 的答案中的模式更优雅一些。我用他的正则表达式更新了正则表达式。
回答by Trinimon
It's because .*
is greedy. Use this instead:
那是因为.*
贪心。改用这个:
"!style_delete\s*\[[^\]]*\]"
It means: match everything in bracket excluding a closing ]
.
这意味着:匹配括号中的所有内容,但不包括结尾]
.
Or make the content between []
it non-greedy:
或者使它之间的内容[]
非贪婪:
"!style_delete\s*\[.*?\]"
回答by Guillaume
You need to use non-greedymatching:
您需要使用非贪婪匹配:
start.*?end
In your case, pattern is :
在你的情况下,模式是:
!style_delete\s\[(.*?)\] (Even simple to understand than first version :))
Proof (Java 7) :
证明(Java 7):
String string = "someother text !style_delete [company code : 43ev4] between text !style_delete [organiztion : 0asj9] end of line text";
Pattern pattern = Pattern.compile("!style_delete\s\[(.*?)\]");
Matcher matcher = pattern.matcher(string) ;
while (matcher.find()) {
System.out.println(matcher.group());
}
Link to proof : http://ideone.com/Qtymb3
证明链接:http: //ideone.com/Qtymb3