I want to extract certain sentences from a paragraph looking at a certain set of words Object C Statement:
. The paragraph is as follows:
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.
I want to extract Object C Statement: as follows:
There was a dolphin with a bag full of bubbles. It was a purple
dolphin with an orange hat.
All examples that I have come across are with splitting a specific word etc.
I tried this, but it doesn't work for me:
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"Bs(?=[^s:]+:)", word)
regex = re.compile(r"""Object A Statements(.*?)Object B Statements(.*?)Object C Statements(.*?)Object D Statements(.*?)Object E Statements(.*)""", re.S|re.X)
a, b, c, d, e = regex.match(word).groups()
question from:
https://stackoverflow.com/questions/65908489/how-to-extract-a-certain-sentence-in-a-paragraph-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…