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
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("),("));
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]
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...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.