tags:

views:

21

answers:

2

This is the (non-escaped) regex i'm using so far

\{<<"(\w+)">>, \[(<<"(\w+)">>,?)+\]\}.

To match this:

{<<"user_1">>, [<<"user_2">>,<<"user_3">>,<<"user_04">>]}.

And these are the groups I'm getting:

1: user_1
2: <<"user_04">>
3: user_04

Any thoughts on why it isn't giving the multiple users?

If you were wondering the file format is erlang based.

+2  A: 

The pattern's group count is fixed at 3. The groups capture the text at the last location where they match. The last two (nested) groups match three times in order to consume the input; you are seeing where they last matched, with the fourth user.

What are you trying to do here? If you want to just match the contents of the <</>> delimiters, you could try something like:

String text = "{<<\"user_1\">>, [<<\"user_2\">>,<<\"user_3\">>,<<\"user_04\">>]}";
String regex = "<<\"(\\w+)\">>";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while (m.find()) {
  System.out.format("found: %s\n", m.group(1));
}

This generates the output:

found: user_1
found: user_2
found: user_3
found: user_04
Jeremy W. Sherman
A: 

I couldn't get the regex to work the way the OP wanted so I offer this lovely thing.

String in =  "{<<\"user_1\">>, [<<\"user_2\">>,<<\"user_3\">>,<<\"user_04\">>]}";
List<String> list = new ArrayList<String>();

list.addAll(Arrays.asList(in.replaceAll("[\\s\\{\\}\\[\\[\\]<>\"]", "").split(",")));

for (String s : list) {
    System.out.println(s);
}
Tony Ennis