This is a very contrived example, but I've searched for things like "regex capture repetition match" and so forth with no luck.
How to get all captures of subgroup matches with preg_match_all()? is the nearest I got.
Rather than an example, here's (sort of) my problem.
I have a tag in the form:
name>>thing1(d1),thing2(d2),thing3(d3)::otherName
I want to extract the name
, the thing
s with their data (one argument at most) and the bit at the end, the otherName
A rule to do this might look something like:
^([a-z]+)>>(([a-z]+\([a-z]+\)(,[a-z]+\([a-z]+\))*)?::([a-zA-Z]]+)$
(This rule wont actually work, I'm missing the numbers, but you should get a feel for the form)
As you can see I'm actually matching my pattern here, I want to pull out the chunks matched by the repetition with the *
Incase it isn't clear since the edit
I am not having trouble matching my tags. I want to extract all parts of the tag in one step. So I want an array like:
Array(`name`,Array(`thing1`,`d1`),Array(Array(`thing2`,`d2`),
Array(`thing3`,`d3`)),`otherName`)
I do have a fallback
I want to do this in one expression as I see no technical reason to not be able to do this. However as a "plan B" I can just extract the chunk between >>
and the ::
and use preg_match_all
- I pose this question because performance is at the back of my mind and my rule already looks at the information, I just have to capture it. So I wouldn't say it's a premature optimisation.