0

I am trying to parse two levels of structure in a string. The input can of form.
x1:x2:x3::y1:y2:y3 --> where there can be more chaining with ::

The regex should be able to parse the string through :: in the first go, and then each group through :.
Output should look like:
x1, x2, x3 and y1, y2, y3

I have been able to figure out parsing one level by capturing the group.

(^[a-zA-Z\d]+):([a-zA-Z\d]+):([a-zA-Z\d]+$)

How do we make it work for two levels of parsing?

On the lighter note, would you prefer just using Split to make it simpler? I prefer regex as it has cleaner checks on data organization.

4
  • 1
    What is the actual output you want here? This sort of would determine which approach to take. Commented Oct 16, 2020 at 7:28
  • 1
    Perhaps like this \G([a-zA-Z\d]+)(?:::?|$) regex101.com/r/hW09yU/1 Commented Oct 16, 2020 at 7:33
  • @Thefourthbird, that kind of loses the essence of multiple level of parsing. I would have to run a loop and parse every three groups together. It also doesn't enforce that there should be three strings separated by a ":" Commented Oct 16, 2020 at 7:49
  • About the split() part: you can still do a .split("::") for getting the variable amount of triplets, and then do the (^...):(...):(...$) on each of them for verification. Commented Oct 16, 2020 at 14:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.