This is a variant on this question and this other question (by myself).
I have a string that I need to parse using regex. The string is something like:
The XXX is blue.
The XXX is blue,
and the YYY is green.
The XXX is blue,
and the YYY is green.
The XXX is blue.
The XXX is blue.
The XXX is blue.
The XXX is blue.
The XXX is blue,
and the YYY is green.
The code above represents one single string, including line feeds. Note how some sentences are followed by an optional subclause after a comma. In those two-part sentences, the YYY "belongs to" the preceding XXX.
I need to match all the XXX and their corresponding YYY, so the result should look something like:
[1][1] XXX
[1][2]
[2][1] XXX
[2][2] YYY
[3][1] XXX
[3][2] YYY
[4][1] XXX
[4][2]
[5][1] XXX
[5][2]
etc.
XXX and YYY could be any character (".*")
How can I write a regex that will match both XXX and YYY? (Remember, YYY could be optional. I use PHP.)