【发布时间】:2016-03-30 22:11:06
【问题描述】:
我有这个简单的输入
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.
【问题讨论】:
标签: perl shell text-processing text-parsing