gpt4 book ai didi

Python3: how to use regular expression to replace multiline(如何使用正则表达式替换多行)

转载 作者:bug小助手 更新时间:2023-10-24 21:35:19 31 4
gpt4 key购买 nike



I have a text file poem.txt contains several lines that I want to search starting with string #---START--- and #---END---. I would like to replace that section with new content. Here is a simple example:

我有一个文本文件poem.txt,其中包含我想要搜索的几行字符串#-Start-和#-End-。我想用新的内容替换那个部分。下面是一个简单的例子:


poem.txt

Poem.txt


Five Little Flowers

Five little flowers growing in a row.
The first one said, "I'm pupple, you know."
The second one said,
"I'm pink as pink can be."
The third one said, "I'm blue like the sea."
The fourth one said,
"I'm a very red fellow."
The fifth one said, "My color is yellow."

Then out came the sun, big and bright.
And five little flowers smiled in delight.

#---START---
It Must Be Spring
Hush, can you hear it?
The rustling in the grass,
Bringing you the welcome news that
Winter’s day is past.
Soft, can you feel it?
The warm caressing breeze,
Telling you the sticky buds
Are bursting on the trees.
Look, can you see them?
The primrose in the lane.
Now you must believe it —
Spring is here again.
#---END---

Spring Song
Frogs croak
Rains soak
Chicks peep
Crickets leap
Bees hum
Robins come
Birds sing
It’s spring!

new content is.

新的内容是。


Rain

The rain is raining all around,
It falls on field and tree,
It rains on the umbrellas here,
And on the ships at sea.

I am having some trouble with the right approach to this replace. find a way is to replace it

我在寻找正确的替代方法方面遇到了一些麻烦。想办法把它替换掉


更多回答
优秀答案推荐

Here is an example how you can use re.sub to perform the substitution (regex101 demo):

以下是如何使用re.Sub执行替换的示例(regex101演示):


import re

poem_txt = """\
Five Little Flowers

Five little flowers growing in a row.
The first one said, "I'm pupple, you know."
The second one said,
"I'm pink as pink can be."
The third one said, "I'm blue like the sea."
The fourth one said,
"I'm a very red fellow."
The fifth one said, "My color is yellow."

Then out came the sun, big and bright.
And five little flowers smiled in delight.

#---START---
It Must Be Spring
Hush, can you hear it?
The rustling in the grass,
Bringing you the welcome news that
Winter’s day is past.
Soft, can you feel it?
The warm caressing breeze,
Telling you the sticky buds
Are bursting on the trees.
Look, can you see them?
The primrose in the lane.
Now you must believe it —
Spring is here again.
#---END---

Spring Song
Frogs croak
Rains soak
Chicks peep
Crickets leap
Bees hum
Robins come
Birds sing
It’s spring!"""

new_content = """\
Rain

The rain is raining all around,
It falls on field and tree,
It rains on the umbrellas here,
And on the ships at sea."""

new_text = re.sub(r"#---START---.*?#---END---", new_content, poem_txt, flags=re.S)
print(new_text)

Prints:

打印:


Five Little Flowers

Five little flowers growing in a row.
The first one said, "I'm pupple, you know."
The second one said,
"I'm pink as pink can be."
The third one said, "I'm blue like the sea."
The fourth one said,
"I'm a very red fellow."
The fifth one said, "My color is yellow."

Then out came the sun, big and bright.
And five little flowers smiled in delight.

Rain

The rain is raining all around,
It falls on field and tree,
It rains on the umbrellas here,
And on the ships at sea.

Spring Song
Frogs croak
Rains soak
Chicks peep
Crickets leap
Bees hum
Robins come
Birds sing
It’s spring!

更多回答

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com