我正在尝试使用 strsplit()
在 R 中基于逗号将字符串分成几部分,但我不想在括号中拆分任何内容。我认为答案是一个正则表达式,但我正在努力使代码正确。
例如:
x <- "This is it, isn't it (well, yes)"
> strsplit(x, ", ")
[[1]]
[1] "This is it" "isn't it (well" "yes)"
当我想要的是:
[1] "This is it" "isn't it (well, yes)"
最佳答案
我们可以使用 PCRE
正则表达式为 FAIL
任何 ,
接下来是 (
之前)
并由 ,
拆分后跟 0 个或多个空格( \\s*
)
strsplit(x, '\\([^)]+,(*SKIP)(*FAIL)|,\\s*', perl=TRUE)[[1]]
#[1] "This is it" "isn't it (well, yes)"
关于regex - 在 R 中使用 strsplit(),忽略括号中的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347537/