5

How to split this String in java such that I'll get the text occurring between the braces in a String array?

GivenString = "(1,2,3,4,@) (a,s,3,4,5) (22,324,#$%) (123,3def,f34rf,4fe) (32)"

String [] array = GivenString.split("");

Output must be:

array[0] = "1,2,3,4,@"
array[1] = "a,s,3,4,5"
array[2] = "22,324,#$%"
array[3] = "123,3def,f34rf,4fe"
array[4] = "32" 
1
  • 1
    Use this pattern "\\((.*?)\\)". Then $1 is containing what you need. Commented May 13, 2016 at 14:01

3 Answers 3

7

You can try to use:

Matcher mtc = Pattern.compile("\\((.*?)\\)").matcher(yourString);
Sign up to request clarification or add additional context in comments.

3 Comments

Use: while (mtc.find()) { String match = mtc.group(1); /* ... */ }.
For performance, instead of a reluctant quantifier (*?), use a greedy quantifier that cannot match close-parenthesis: "\\(([^)]*)\\)"
Can I find number of items the matcher (mtc) contains without using the while loop (or any loop)? In order to the define the size of the String array.
2

The best solution is the answer by Rahul Tripathi, but your question said "How to split", so if you must use split() (e.g. this is an assignment), then this regex will do:

^\s*\(|\)\s*\(|\)\s*$

It says:

  • Match the open-parenthesis at the beginning
  • Match close-parenthesis followed by open-parenthesis
  • Match the close-parenthesis at the end

All 3 allowing whitespace.

As a Java regex, that would mean:

str.split("^\\s*\\(|\\)\\s*\\(|\\)\\s*$")

See regex101 for demo.

The problem with using split() is that the leading open-parenthesis causes a split before the first value, resulting in an empty value at the beginning:

array[0] = ""
array[1] = "1,2,3,4,@"
array[2] = "a,s,3,4,5"
array[3] = "22,324,#$%"
array[4] = "123,3def,f34rf,4fe"
array[5] = "32"

That is why Rahul's answer is better, because it won't see such an empty value.

Comments

0

Usually, you would want to use the split() function as this is the easiest way to split a string into multiple arrays when the string is broken up by a key char.

The main problem is that you need information inbetween two chars. The easiest way to solve this problem would to go through the string get ride of every instance of '('. This leaves the string looking like

String = "1,2,3,4,@) a,s,3,4,5) 22,324,#$%) 123,3def,f34rf,4fe) 32)"

And this is perfect, as you can split by the char ')' and not worry about the other bracket interfering with the split. I suggest using the replace("","") where it replaces every instance of the first parameter with the second parameter (we can use "" to delete it).

Here is some example code that may work :

String a = "(1,2,3,4,@) (a,s,3,4,5) (22,324,#$%) (123,3def,f34rf,4fe) (32)" 
a = a.replace("(","");
//a is now equal to 1,2,3,4,@) a,s,3,4,5) 22,324,#$%) 123,3def,f34rf,4fe) 32)
String[] parts = a.split("\\)");

System.out.println(parts[0]); //this will print 1,2,3,4,@

I haven't tested it completely, so you may end up with unwanted spaces at the end of the strings you may need to get rid of!

You can then loop through parts[] and it should have all of the required parts for you!

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.