gsub 提取两行之间的相关内容
Gsub to extract relevant content between two lines
我想提取 Preparation 和 Nutrition\nPer serving 之间的内容。
我正在使用 -
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
如果要提取内容,最好使用str_extract
或regmatches
功能。
> 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."
我想提取 Preparation 和 Nutrition\nPer serving 之间的内容。 我正在使用 -
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
如果要提取内容,最好使用str_extract
或regmatches
功能。
> 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."