2015-09-18 30 views
0

我有一個很多文字的列,我只想保留之間的文字[start section id="20107"][end section id="20107"]其餘不重要。R我如何保留兩個indetifiers之間的字符串

這裏的原始數據

[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"] 

我所試圖做的就是保持起始區段編號之間的文本= 「20107」 和結束部分ID = 「20107」

[start section id="20107"] 

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

[end section id="20107"] 

任何幫助在這個話題非常感謝。

+0

你嘗試過自己嗎?以下是如何創建一個[可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Heroka

+0

@Heroka,我試過'testdf = filter( org_df,grepl('[start section id =「20107」] | [end section id =「20107」]',col1))',我沒有得到正確的結果,它顯示了原始列,並沒有擺脫這些開始和結束條件之外的文本 –

+0

請提供一些示例數據並添加您嘗試的答案。並且grepl會爲整個字符串內的匹配返回一串布爾值。你可能需要gsub。 – Heroka

回答

0

您可以使用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"] 
+0

沒有過濾開始和結束點之間的文本,它只是顯示了與之前相同的列,沒有更改 –

+0

現在檢查...... perl = TRUE是非常需要的。 –

+0

沒有區別:)和以前一樣,顯示所有內容 –

相關問題