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 [ ,

2 Answers 2

3

First, you don't want to split on the brackets. You just want to exclude them from your end result. So first thing you'll probably want to do is strip those out:

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

Then you can do a basic split on the comma:

String[] notifySplit = notifyWho.split(",");
Sign up to request clarification or add additional context in comments.

Comments

2

I would do it in one line, first removing the square brackets, then splitting:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.