-5

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?

hanchao
  • 41
  • 6

2 Answers2

2

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

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.

David542
  • 104,438
  • 178
  • 489
  • 842