R 3.0 lookbehind不起作用

时间:2013-08-02 17:55:37

标签: regex r lookahead lookbehind

我正在尝试使用具有前瞻/后视的正则表达式从医疗报告中捕获文本。

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,但我能找到的所有示例都是语法。有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

发现问题:

perl = TRUE解决了这个问题。