我想提取"a", "b"
的["a", "b"]
,其中内的内容[...]
所做的操作之前是未知的。[...]
唯一的标识符也是如此。
通常,提取的工作方式是
stringr::str_match(string = ["a", "b"]', pattern = "LEFT(.*?)RIGHT")
所以我必须顺手找到:
stringr::str_match(string = '["a", "b"]', pattern = "[(.*?)]")
但我必须逃避括号。
stringr::str_match(string = '["a", "b"]', pattern = "[[](.*?)[]]")
可能现在括号已转义,但不是(.*?)
吗?
我试过的
通常,我会[
像这样逃避括号[[]
并使用perl = TRUE。因此,我尝试:
stringr::str_match(string = '["a", "b"]', pattern = "[[](.*?)[]]")
如上图所示。
使用表达式像/[^[\]]+\[[^[\]]+\]/
从括号内提取字符串
我们可以使用str_replace
它将直接提取元素
library(stringr)
str_replace(str2, "\\[([^]]+)\\].*", "\\1")
#[1] "\"a\", \"b\""
或搭配 str_match
str_match(str2, "\\[([^]]+)")[,2]
#[1] "\"a\", \"b\""
str2 <- '["a", "b"]'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句