Рандомизация текста между разделителями

У меня есть этот простой ввод

I have {red;green;orange} fruit and cup of {tea;coffee;juice}

Я использую Perl для определения закономерностей между двумя внешними разделителями скобок{ а также}и рандомизируйте поля внутри с помощью внутреннего разделителя;.

Я получаю этот вывод

I have green fruit and cup of coffee

Это мой рабочий скрипт на Perl

perl -plE 's!\{(.*?)\}!@x=split/;/,$1;$x[rand@x]!ge' <<< 'I have {red;green;orange} fruit and cup of {tea;coffee;juice}'

Моя задача - обработать этот формат ввода

I have { {red;green;orange} fruit ; cup of {tea;coffee;juice} } and {nice;fresh} {sandwich;burger}.

Как я понял, скрипт должен пропускать внешние закрывающие скобки{ ... } в первой части текста, внутри которой есть текст с открывающими и закрывающими скобками:

{ {red;green;orange} fruit ; cup of {tea;coffee;juice} }

Следует выбрать случайную часть, как это

{red;green;orange} fruit

или же

cup of {tea;coffee;juice}

Тогда это идет глубже:

green fruit

После того, как весь текст обработан, результатом может быть любой из следующих

I have red fruit and fresh burger.
I have cup of tea and nice sandwich
I have green fruit and nice burger.
I have cup of coffee and fresh burger.

Скрипт также должен анализировать и рандомизировать следующий текст. Например

This {beautiful;perfect} {image;photography}, captured with the { {NASA;ESA} Hubble Telescope ; {NASA;ESA} Hubble Space Telescope} }, is the {largest;sharpest} image ever taken of the Andromeda galaxy { {— otherwise known as M31;— known as M31}; [empty here] }.
This is a cropped version of the full image and has 1.5 billion pixels. { You would need more than {600;700;800} HD television screens to display the whole image. ; If you want to display the whole image, you need to download more than {1;2} Tb. traffic and use 800 HD displays }

Пример вывода может быть

This beautiful image, captured with the NASA Hubble Telescope, is the
sharpest image ever taken of the Andromeda galaxy — otherwise known as
M31.
This is a cropped version of the full image and has 1.5 billion
pixels. You would need more than 700 HD television screens to display
the whole image.

Ответы на вопрос(3)

Ваш ответ на вопрос