1

I cant seem to be able to split on a simple regex,

If i have a string [data, data2] and i attempt to split like so: I tried to escape the brackets.

    String regex = "\\[,\\]";
    String[] notifySplit = notifyWho.split(regex);

The output of looping through notifySplit shows this regex not working

notify: [Everyone, Teachers only]

Any help on what the proper regex is, i am expecting an array like so: data, data2

where i could possibly ignore these two characters [ ,

4

2 回答 2

3

首先,您不想在括号上拆分。您只想将它​​们从最终结果中排除。所以你可能想做的第一件事就是把它们去掉:

notifyWho = notifyWho.replace("[", "").replace("]", "");

然后您可以对逗号进行基本拆分:

String[] notifySplit = notifyWho.split(",");
于 2012-07-26T01:44:32.750 回答
2

我会在一行中完成,首先删除方括号,然后拆分:

String[] notifySplit = notifyWho.replaceAll("[[\\]]", "").split(",");
于 2012-07-26T02:07:32.133 回答