Akyl
Akyl

Reputation: 349

Java Matcher Error

My string:

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

I am trying to obtain every data between [<--->] brackets. Here's my code.

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();
     }

The first results are:

start = 10
end = 11
group = "."

What I wanted was (Counting on my head)

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

Next is

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

and so on

What is the correct pattern?

Upvotes: 1

Views: 173

Answers (1)

anubhava
anubhava

Reputation: 785196

You're using wrong escaping. Use this regex:

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

EDIT: Based on your comment:

If you want to capture all items inside square brackets just run your while loop like this:

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

Upvotes: 1

Related Questions