I have a quite silly problem, which is staggering me for a while...
I want to parse some text, formatted this way:
CUT-FROM-A ...
CUT-FROM-B ...
CUT-TO ...
CUT-TO
apple
CUT-FROM-C ...
CUT-TO
orange
In this example, I would like to extract the 'fruits', ignoring everything from CUT-FROM-X
to the corresponding TO
. By 'corresponding' I mean "from inside to outside", or if it's clearer, try mentally substiting any CUT-FROM-A
with an open bracket, and any CUT-TO
with a closed bracket: then, I want to ignore the content inside the brackets, including the brackets.
I hope this is clear, but I'm afraid it's not... :-(
I suppose the main difficulty here is that the 'closing brackets' all have the same signature, so can't be easily associated with the relative opener...
I have tried something like this (not greedy):
$output_text = preg_replace("/CUT-FROM-.*?TO/s", "", $input_text);
but this leaves the second CUT-TO
in the output...
And something like this (greedy):
$output_text = preg_replace("/CUT-FROM-.*TO/s", "", $input_text);
but this eats the first 'fruit'... :-(
This is my testing on regex101.
Anybody can shed some light on me?