2013-04-04 70 views
1

我的字符串:Java的匹配器错误

null[00:14.04]I've /n[00:14.11]got /n[00:14.18]a /n[00:14.25]fee- /n[00:15.02]ling /n 

我试图获得之间的每一个数据[< --->]括号中。这是我的代码。

String find = "[(.*?)\\\\]"; 
Pattern patern = Pattern.compile(find); 
Matcher matcher = patern.matcher(intake); 
    while(matcher.find()){ 
     i++; 
     matcher.find(i); 
     int start = matcher.start(); 
     int end = matcher.end(); 
     String group = matcher.group(); 
    } 

初步结果:

start = 10 
end = 11 
group = "." 

我想要的是(在我的头上计数)

start = 4 
end = 14 
group = [00:14.04] 

接下来是

start = 22 
end = 32 
group = [00:14.11] 

什么是正确的模式?

+1

所以你希望它匹配并返回'[]'括号内的所有内容,那么所有的时间戳?你在“想要”中说什么不清楚。 – Walls 2013-04-04 14:31:25

回答

1

您正在使用错误的转义。使用此正则表达式:

String find = "\\[(.*?)\\]"; 

编辑:基于您的评论:

如果你想捕捉方括号内的所有项目只运行while循环是这样的:

while(matcher.find()) { 
    String matched = matcher.group(1); 
    System.out.printf("Matched Group: [%s]%n", matched); 
} 
+0

我希望匹配括号内的所有项目。抱歉让我更新我的问题。 – Akyl 2013-04-04 14:34:53

+0

@Akyl:这将匹配正确的方括号内的字符串,它会产生**精确的**开始,结束,组的值,你所期望的。 – anubhava 2013-04-04 14:36:20

+0

对不起,你是对的我在我的循环中犯了一个错误,重复它的过程我的不好。谢谢btw。 – Akyl 2013-04-04 14:47:55