开发者

Java regex to match odd file format: {<<"user_1">>, [<<"user_2">>,<<"user_3">>,<<"user_04">>]}

This is the (non-escaped) rege开发者_如何学Pythonx 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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜