R 如何保留两个标识符之间的字符串

R how do I retain string between two indetifiers

我有一个包含很多文本的专栏,我只想保留 [start section id="20107"][end section id="20107"] 之间的文本,其余的并不重要。

这里是原始数据

[start section id="20106"]

California, Death Valley 

[end section id="20106"]

[start section id="20107"]

1. Apple
2. Orange
3. Bannana
4. Kiwi
5. Grapes
6. Strawberry

[end section id="20107"]


[start section id="20108"]

Jose has worked on these farms , currently he is in Florida picking tomatos

[end section id="20108"]

我想做的只是保留开始部分 id="20107" 和结束部分 id="20107" 之间的文本

[start section id="20107"]

1. Apple
2. Orange
3. Bannana
4. Kiwi
5. Grapes
6. Strawberry

[end section id="20107"]

非常感谢有关此主题的任何帮助。

您可以使用sub

x <- '[start section id="20107"]

1. Apple
2. Orange
3. Bannana
4. Kiwi
5. Grapes
6. Strawberry

[end section id="20107"]


[start section id="20108"]

Jose has worked on these farms , currently he is in Florida picking tomatos

[end section id="20108"]'
cat(sub('[\s\S]*(\[start section id="20107"\][\s\S]*?\[end section id="20107"\])[\s\S]*', '\1', x, perl=T))

#[start section id="20107"]

#1. Apple
#2. Orange
#3. Bannana
#4. Kiwi
#5. Grapes
#6. Strawberry

#[end section id="20107"]