【发布时间】:2021-04-30 15:48:22
【问题描述】:
我想从一个段落中提取某些句子,查看一组单词Object C Statement:。段落如下:
对象 A 陈述:有一只猫带着一袋肉。那是个 戴着蓝色帽子的红猫。对象 B 陈述:有一只狗 装满玩具的袋子。那是一只戴着绿帽子的蓝狗。对象 C 陈述:有一只海豚,袋子里装满了泡泡。那是个 戴着橙色帽子的紫色海豚。对象 D 陈述:有一个 满袋草的斑马。那是一匹戴着蓝色帽子的白色斑马。 对象 E 陈述:有一只熊带着一个装满木头的袋子。那是个 戴着黑帽子的棕熊。
我要提取Object C语句:如下:
有一只海豚,袋子里装满了泡泡。那是一个紫色 戴着橙色帽子的海豚。
我遇到的所有示例都是拆分特定单词等。
我试过了,但它对我不起作用:
word="Object A Statement: There was a cat with a bag full of meat. It was a red cat with a blue hat. Object B Statement: There was a dog with a bag full of toys. It was a blue dog with a green hat. Object C Statement: There was a dolphin with a bag full of bubbles. It was a purple dolphin with an orange hat. Object D Statement: There was a zebra with a bag full of grass. It was a white zebra with a blue hat. Object E Statement: There was a bear with a bag full of wood. It was a brown bear with a black hat."
a, b, c, d, e = re.split(r"\B\s(?=[^\s:]+:)", word)
regex = re.compile(r"""Object A Statement\s(.*?)Object B Statement\s(.*?)Object C Statement\s(.*?)Object D Statement\s(.*?)Object E Statement\s(.*)""", re.S|re.X)
a, b, c, d, e = regex.match(word).groups()
【问题讨论】: