首页 > 解决方案 > 从字符串中提取第一个元素

问题描述

我想从 c([a,b],[c,d]) 的方括号中提取第一个元素。请参阅下面的示例 -

x <- "[\"Multi & Flexi-Cap\", \"Multi & Flexi Cap Fund\"], [\"Large-Cap\", \"Large Cap Fund\"], [\"Large & Mid-Cap\", \"Large & Mid Cap Fund\"], [\"Mid-Cap\", \"Mid Cap Fund\"], [\"Small-Cap\", \"Small Cap Fund\"], [\"ELSS\", \"ELSS (Tax Savings)\"], [\"Dividend Yield\", \"Dividend Yield\"], [\"Equity - Sectoral\", \"Sectoral/Thematic\"], [\"Contra\", \"Contra Fund\"], [\"Focused Fund\", \"Focused Fund\"], [\"Value\", \"Value Fund\"], [\"RGESS\", \"RGESS\"], [\"Equity - Other\", \"Equity - Other\"]"

前 3 个括号的期望输出 [ ]

c("Multi & Flexi-Cap", "Large-Cap", "Large & Mid-Cap")

标签: r

解决方案


使用stringr你可以尝试:

x <- "[\"Multi & Flexi-Cap\", \"Multi & Flexi Cap Fund\"], [\"Large-Cap\", \"Large Cap Fund\"], [\"Large & Mid-Cap\", \"Large & Mid Cap Fund\"], [\"Mid-Cap\", \"Mid Cap Fund\"], [\"Small-Cap\", \"Small Cap Fund\"], [\"ELSS\", \"ELSS (Tax Savings)\"], [\"Dividend Yield\", \"Dividend Yield\"], [\"Equity - Sectoral\", \"Sectoral/Thematic\"], [\"Contra\", \"Contra Fund\"], [\"Focused Fund\", \"Focused Fund\"], [\"Value\", \"Value Fund\"], [\"RGESS\", \"RGESS\"], [\"Equity - Other\", \"Equity - Other\"]"

library(stringr)

unlist(str_extract_all(x, "(?<=\\[\\\\?[:punct:])[A-z &-]*(?=\\\\?)"))
#>  [1] "Multi & Flexi-Cap" "Large-Cap"         "Large & Mid-Cap"  
#>  [4] "Mid-Cap"           "Small-Cap"         "ELSS"             
#>  [7] "Dividend Yield"    "Equity - Sectoral" "Contra"           
#> [10] "Focused Fund"      "Value"             "RGESS"            
#> [13] "Equity - Other"

reprex 包于 2021-08-27 创建 (v2.0.0 )


推荐阅读