在Java中,你不能赶上多个匹配成一个组,即不能使用(?:([^:]+):)+
收集lvt
,vgt
等成一个组一个去。
你可以做的是打破了文成各行,如果你知道总会有4组,调用find()
然后group(0)
4次连续使用这样的表达式:[^:]+
。这应该在4次调用中捕获lvt
,vgt
,mwi-ao
和44.00m
。
一些伪(未测试,因此它可能包含错别字:)):
Pattern p = Pattern.compile("[^:]+");
String input = ...;
String[] lines = input.split("\\s");
for(String line : lines) {
//note that for simple cases like above you could also just split by ":"
Matcher m = p.matcher(line);
List<String> elements = new LinkedList<String>();
while(m.find()) {
elements.add(m.group(0));
}
//get the first 4 elements from the list
//if there are less then 4 in the list, the line didn't match
}
编辑:我更新了答案,以匹配编辑的问题,这似乎划“线”我的空白和领域由冒号(:
)。
您使用的是'.match()'或'的.find()'? – npinti 2012-07-24 08:32:56
如果文本中没有“:”匹配,为什么在组中有':'?除此之外,如果你想抓住4个组中的每一个组,AFAIK就没有办法在Java中缩短这个表达式。 – Thomas 2012-07-24 08:34:49
我粘贴了不正确的文字。 – 2012-07-24 08:48:14