1

Here is the scenario, I have this regex pattern:

\"category\",([0-9]+)\n(\"subcategory\",[0-9]+\n)*

this pattern should match the following data:

"category",1
"subcategory",1
"subcategory",2
"subcategory",3
"category",2
"subcategory",1
"subcategory",2
"subcategory",3

and I'm using the following regex function:

public static List<String> regexFindMultiStrings(String pattern, String input) {
    Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(input);
    List<String> data = new ArrayList<String>() ;

    while (m.find()) 
    {

         for (int i = 0; i <= m.groupCount(); i++) 
         {
             data.add(m.group(i));
             //Log.e("Array", m.group(i));
         }
    }
    return data;
}

here is the problem, now when I use this pattern to match all the data it gives only the following:

1
"subcategory",1
2
"subcategory",1

which is something I'm not looking for how to get all of the data something like:

1
"subcategory",1
"subcategory",2
"subcategory",3
2
"subcategory",1
"subcategory",2
"subcategory",3
Desolator
  • 22,411
  • 20
  • 73
  • 96
  • same thing the problem is if I use regexpal or any other regex utility I get all string matched but with java I only get the main category and the first subcategory. its a weird behavior :\ – Desolator Dec 09 '12 at 07:35
  • See http://stackoverflow.com/questions/5018487/regular-expression-with-variable-number-of-groups – Mat Dec 09 '12 at 07:42

1 Answers1

3

You are missing a pair of parentheses:

\"category\",([0-9]+)\n((\"subcategory\",[0-9]+\n)*)

The problem lies in the fact that you can not expect to obtain the capture of multiple matches of the same group.

Optionally you can make the inner group non-capturing:

\"category\",([0-9]+)\n((?:\"subcategory\",[0-9]+\n)*)
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
CAFxX
  • 28,060
  • 6
  • 41
  • 66