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
4

1 回答 1

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)*)
于 2012-12-09T07:40:58.393 回答