我正在尝试使用带有前瞻/后视的正则表达式从医疗报告中捕获文本。
lookahead 工作正常,lookbehind 根本不起作用:
library(plyr)
library(tm)
library(stringr)
library(gsubfn)
d1 <- c("CCA: 135 cm/sec ICA:", "CCA: 150 cm/sec ICA:")
d1
[1] "CCA: 135 cm/sec ICA:" "CCA: 150 cm/sec ICA:"
# Lookahead works
d1$sub1 <- lapply((strapply(d1,".*?(?=ICA:)")), unique)
Warning message:
In d1$sub1 <- lapply((strapply(d1, ".*?(?=ICA:)")), unique) :
Coercing LHS to a list
d1
[[1]]
[1] "CCA: 135 cm/sec ICA:"
[[2]]
[1] "CCA: 150 cm/sec ICA:"
$sub1
$sub1[[1]]
[1] "CCA: 135 cm/sec " ""
$sub1[[2]]
[1] "CCA: 150 cm/sec " ""
# lookbehind fails
d1$sub2 <- lapply((strapply(d1,"(?<=CCA:).*?(?=ICA:)")), unique)
错误:
Error in structure(.External("dotTcl", ..., PACKAGE = "tcltk"), class = "tclObj") :
[tcl] couldn't compile regular expression pattern: quantifier operand invalid.
我知道有一条关于使用 lapply 的错误消息 - 但由于前瞻有效,看起来后向似乎也应该如此。
也许我错误地使用了lookbehind,但我能找到的所有示例都是语法。有什么帮助吗?