我正在尝试使用具有前瞻/后视的正则表达式从医疗报告中捕获文本。
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。
也许我错误地使用了lookbehind,但我能找到的所有示例都是语法。有什么帮助吗?
答案 0 :(得分:3)
发现问题:
perl = TRUE解决了这个问题。