Gsub提取两行之间的相关内容

时间:2015-07-10 05:30:12

标签: regex r gsub

我想提取准备和营养\ nPer服务之间的内容。 我正在使用 -

gsub(".*\nPreparation\n\\s*|Tips & Notes*", "", filename)

我的文件名看起来像

    \nPreparation\nThinly slice both lemons and one orange. Combine the 
    sliced fruit with rum in a bowl. Cover and let macerate at least 8 hours 
or overnight.\nCombine sugar and water in a small saucepan; bring to a boil. Remove from the heat and stir in tea; let steep for 20 to 30 minutes. Strain into the rum mixture. Cover and chill.\nJust before serving, slice the remaining orange. Strain the rum mixture into a large punch bowl. Add Champagne or sparkling wine and seltzer. Float the orange slices in the punch.\nTips & Notes\nMake Ahead Tip: Prepare through Step 2. Cover and refrigerate for up to 3 days.\nNutrition\nPer serving 

1 个答案:

答案 0 :(得分:3)

如果要提取内容,最好使用str_extractregmatches功能。

> regmatches(x, gregexpr("(?s)Preparation\\s*\\K.*?(?=\\s*\\bNutrition\\nPer serving)", x, perl=T))[[1]]
[1] "Thinly slice both lemons and one orange. Combine the \n        sliced fruit with rum in a bowl. Cover and let macerate at least 8 hours \n    or overnight.\nCombine sugar and water in a small saucepan; bring to a boil. Remove from the heat and stir in tea; let steep for 20 to 30 minutes. Strain into the rum mixture. Cover and chill.\nJust before serving, slice the remaining orange. Strain the rum mixture into a large punch bowl. Add Champagne or sparkling wine and seltzer. Float the orange slices in the punch.\nTips & Notes\nMake Ahead Tip: Prepare through Step 2. Cover and refrigerate for up to 3 days."