1

I want to split string to brackets ( [] and () ), however I didn't find a good solution. Splitting them to "[" and "(" didn't really help. Basically I want to turn

[str1 str2] (str3 str4) hello world

to

- [str1 str2]
- (str3 str4)
- hello world

EDIT:

I have this regex now: \s+(?![^[][*]]) But I can't really seem to add () (it ignores stuffs inside [] )

EDIT 2:

Marked as duplicate. My question is to keep strings in bracket together, not to split by symbol.

RobiFoxx
  • 13
  • 5
  • 1
    what if you have [[str1 str2] strA] (str3 str4)??? – ΦXocę 웃 Пepeúpa ツ Jan 13 '17 at 15:25
  • 1
    String::split(regex)? – CraigR8806 Jan 13 '17 at 15:26
  • @ΦXocę웃Пepeúpaツ Should be "[[str1 str2] strA]" and "(str3 str4)". But I wouldn't allow that kind of string. – RobiFoxx Jan 13 '17 at 15:28
  • @CraigR8806 I don't really have experience with regexes. :l – RobiFoxx Jan 13 '17 at 15:30
  • Here: http://stackoverflow.com/questions/9856916/java-string-split-regex – CraigR8806 Jan 13 '17 at 15:36
  • Have you tried using `indexOf`? Although it is not using regex but it's simple. – Hardik Modha Jan 13 '17 at 16:22
  • @CraigR8806 \s+(?![^\[][*\]]) I have this now, works for [], but not for (). @ HardikModha Forgot to mention: There could be more [] or (). – RobiFoxx Jan 13 '17 at 16:25
  • `indexOf` has several overloads. The one I believe Hardik was meaning is `haystack.indexOf(needle, startingPos)` where `startingPos` is the numerical index into the string of which to start looking. In essence you could "leap-frog" from `needle` to `needle` calling `indexOf` multiple times. This can get messy and verbose though, but it is a simpler solution. That's a great start for not being very familiar with RegEx! I will try to work you up a solution – CraigR8806 Jan 13 '17 at 16:34
  • 1
    @CraigR8806 Yeah, I know it's messy and not an efficient one. But if [] or () would occur only single time then it's worth trying. – Hardik Modha Jan 13 '17 at 16:36
  • Yeah, I know that. But I'd have to look for brackets, and ignore others until it's not closed. – RobiFoxx Jan 13 '17 at 16:37
  • Possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – pringi Jan 13 '17 at 16:38

2 Answers2

0

This gets you close (it matches the strings within the square brackets and parentheses, but not the "hello world" at the end:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SplitRegex {

    public static void main(String[] args) {
        // The search string
        String str = "[str1 str2] (str3 str4) hello world";

        // The Regular expression (Finds [(word)] tokens)
        Pattern pt = Pattern.compile("[\\(\\[]([^\\]\\)]*)[\\]\\)]");

        // Match the string with the pattern
        Matcher m = pt.matcher(str);

        // If results are found
        while (m.find()) {
            System.out.println(m.group(0)); 
        }
    }

}
David Choweller
  • 1,060
  • 1
  • 8
  • 7
0

Here is another solution that is a bit more compact:

String str = "[str1 str2] (str3 str4) hello world";
String[] seperatedWords = str.split("\\r+(?:\\(.*\\))|(?:[.*])");
CraigR8806
  • 1,584
  • 13
  • 21