Home > other >  Split string with two brackets
Split string with two brackets

Time:10-06

I have a string that comes out like this:

String line = "2022{yyyy}{mm}{dd}";

I want to split it into an arraylist like this:

2022
yyyy
mm
dd

how can i do this?

CodePudding user response:

One approach:

String line = "2022{yyyy}{mm}{dd}";
String[] parts = line.split("[{}] ");
System.out.println(Arrays.toString(parts));  // [2022, yyyy, mm, dd]

Another possibility might be to do a regex find all on \w using a formal pattern matcher.

CodePudding user response:

Here would be an example approach if you wanted to use four capturing groups:

  • enter image description here

    Having said that, Tim's approach is much simpler and I'd go for that.

  • Related