0

I need to split a string in java with a regex, but i'm not quite sure how to. I have to split

[Caps][RCaps]C[Caps][RCaps]atalog[Caps][RCaps]

By the words inside the square brackets. I somehow need to get all the parts of the string.

Expected output:

[Caps]
[RCaps]
C
[Caps]
[RCaps]
atalog
[Caps]
[RCaps]

And the text inside the square brackets could be whatever. In this case, it is "Caps" and "RCaps" but it could also be "Potato" for example

2
  • 1
    Mind showing us some code? You're really not going to get much help without that. Commented Mar 1, 2015 at 18:26
  • In other words you want to split after ] or before [? So what is the problem with your code? Commented Mar 1, 2015 at 18:27

4 Answers 4

3

What you need to do is to split both before a [ character and after a ] character, This translates into the regex (?=\\[)|(?<=\\]).

Example:

String string = "[Caps][RCaps]C[Caps][RCaps]atalog[Caps][RCaps]";
String[] result = string.split("(?=\\[)|(?<=\\])");
System.out.println(Arrays.toString(result));

This prints:

[[Caps], [RCaps], C, [Caps], [RCaps], atalog, [Caps], [RCaps]]
Sign up to request clarification or add additional context in comments.

7 Comments

Wow, ninja. How do you compile regex in your head?
Note: Before Java 8, the result would contain an empty space as first array element.
@Shahar What do you mean? This solution is kind of first thing which comes to mind after seeing that OP wants to split after ] (?<=\\]), or (|) before [ (?=\\[)?
@Pshemo I still wouldn't trust my memory and have to compile it just in case.
@Shahar After some experience, you'd start trusting your memory, because you wouldn't need it. Regex is about logic, more than memory..
|
1

instead of split, you could use this pattern to capture what you want

(\[[^][]*\]|[^][]+)  

Demo

Comments

1

"[" or "]" are regex matching characters called meta-characters. You need to add escape char before it. \\[ or \\]

You need to try something like this:

String str = "[Caps][RCaps]C[Caps][RCaps]atalog[Caps][RCaps]";
Pattern pattern = Pattern.compile("\\[?(\\w)+\\]?");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
    System.out.print(matcher.group());
}

Output:

[Caps] [RCaps] C [Caps] [RCaps] atalog [Caps] [RCaps]

5 Comments

They are called meta-characters. Btw the OP wants the brackets to be a part of the output..
How do you know OP is even splitting on either of those characters without seeing any code? I mean, yeah that is obvious, but still...
@RohitJain: That seemed obvious to me from his question. Other than meta-character handling regex seems to be straight-forward.
@AnkurShanbhag Not exactly straight-forward, unless you know about look-arounds (mostly a newbie in regex wouldn't know that).
Very useful response, the only reason I chose the other answer over this one is that the other one gives me an array straight away, which is closer to my needs. On the other hands, this solution is better as it works on JRE 1.8 without exception, other than the first solution, which needs a special case if the first element is empty. Using this method, i have to build the array myself, which is simply not as fast (i believe). If i could choose 2 best answers, these two would be it.
1

try this.

 String string = "[Caps][RCaps]C[Caps][RCaps]atalog[Caps][RCaps]";
        String[] split = string.split("\\]");
        for (String string1 : split) {
            if (string1.charAt(0) != '[') {
                int indexOf = string1.indexOf("[");
                String substring = string1.substring(0, indexOf);
                String substring1 = string1.substring(indexOf, string1.length()) + "]";
                System.out.println(substring);
                System.out.println(substring1);
            } else {
                System.out.println(string1 + "]");
            }
        }

3 Comments

This is way to go, I agree, but it's not that "nice-looking", I agree that it works, I've tested it, but using substrings over and over again may be a bit inefficient. However, the response itself is good, and it might come in handy for other uses ;)
there should be any single delimeter or character so that it can be split due to which i used substring.
I understood how your idea works and i just checked it out, it seems rather fast and i already have another idea where to use this ;)

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.