【发布时间】:2021-08-27 18:23:50
【问题描述】:
我有一个看起来相当可行的正则表达式问题,但我似乎无法正确解决。
给出如下所示的字符串:
id1234|a; b; c; d
id5678|a; b; e; f
id9012|a; g; h; i
我正在尝试从管道中捕获固定出现的分号,直觉上我希望是这样的:
"(?<=\\|)(([^;]*;){1}[^;]*).*"
从管道中选择直到第二个分号,因为:
"^(([^;]*;){1}[^;]*).*"
选择从行首到第二个分号。
相信:
(?<=\|)(([^;]*;){1}[^;]*).*
从管道开始正确选择,但似乎无法在正确的分号处结束捕获。
但在 R gsub 中抱怨:
a <- c("id1234|a; b; c; d",
"id5678|a; b; e; f",
"id9012|a; g; h; i",
"id3456|a; j; k; l")
b <- gsub(pattern = "^(([^;]*;){1}[^;]*).*",
replacement = "\\1",
x = a)
b
[1] "id1234|a; b" "id5678|a; b" "id9012|a; g" "id3456|a; j"
c <- gsub(pattern = "(?<=\\|)(([^;]*;){1}[^;]*).*",
replacement = "\\1",
x = a)
Error in gsub(pattern = "(?<=\\|)(([^;]*;){1}[^;]*).*", replacement = "\\1", :
invalid regular expression '(?<=\|)(([^;]*;){1}[^;]*).*', reason 'Invalid regexp'
In addition: Warning message:
In gsub(pattern = "(?<=\\|)(([^;]*;){1}[^;]*).*", replacement = "\\1", :
TRE pattern compilation error 'Invalid regexp'
【问题讨论】: