3

I have strings that I need to parse that look like this

"(1,0,quote),(1,0,place),(1,0,hall),(2,0,wall)"

I want to split the string into chunks of triplets so that I get

1,0,quote 
1,0,place 
1,0,hall 
2,0,wall 

How can I do this with String.split? If I use a comma as a delimeter, it would split the words too. I want to split them using the delimeter "),(". How do I do this?

Thanks

2
  • 3
    Use a regex or write a real parser, if you need to handle arbitrarily-nested ()s. Or use a real serialization that already solves this problem: JSON. Commented Jan 24, 2014 at 21:59
  • 2
    Why not remove the first and last character, then split them by ),( like you said? Commented Jan 24, 2014 at 22:00

3 Answers 3

3

If you split your string with ),( you will not remove ( from start and ) at end of your string. Consider using Pattern and Matcher classes to find elements between ( ).

String text = "(1,0,quote),(1,0,place),(1,0,hall),(2,0,wall)";

Pattern p = Pattern.compile("\\(([^)]+)\\)");
Matcher m = p.matcher(text);
while(m.find()) {
    System.out.println(m.group(1));
}

Output:

1,0,quote
1,0,place
1,0,hall
2,0,wall

If you really want to use split on ),( you will need to manually remove first ( and last ) (splitting will only remove part that should be split on). Also you will have to escape parenthesis ) ( because they are regex metacharacter (for example used to create groups). To do this you can manually add \\ before each of ) and (, or you can surround ),( with \\Q and \\E to mark characters between these elements as literals. BUT you don't have to do this manually. Just use Pattern.quote to produce regex with escaped all metacharacters and use it as split argument like

//I assume that `text` already removed `(` and `)` from its start and end 
String[] array = text.split(Pattern.quote("),("));
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah. I actually made a more specific regex expression (([^,]+[,]){2}[^,]+) . It also rules out instances in which the name has brackets.
@Franzd'Anconia A little optimization for the regexp, [,] can be replaced simply by ,.
2

With a split method, you'll get an array with one empty cell. Use Pattern and Matcher class instead.

Try this code instead:

String s = "(1,0,quote),(1,0,place),(1,0,hall),(2,0,wall)";
Pattern p = Pattern.compile("\\d+,\\d+,[^)]+");
Matcher m = p.matcher(s);

List<String> l=new ArrayList<>();
while(m.find()) {
    l.add(m.group());
}

System.out.println(l);

Output

[1,0,quote, 1,0,place, 1,0,hall, 2,0,wall]

Comments

1

As you mentioned, you can split at ),( - then, when iterating over the result array, you only need to take into account, that array[0] contains an additional ( and array[n-1] contains an additional ).

You could also apply a regex to remove the leading and trailing bracket, first or use substring from 1 to n-2 before splitting, etc...

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.