1

好的——也许这是一个更好的例子。我正在寻找有关如何在正则表达式中引用变量的指导/参考 - 而不是如何为此数据构建正则表达式。

如何使用变量中的值来正则表达式下一个变量?

library(plyr)    
library(tm)
library(stringr)
library(gsubfn)

速度数据集

d1$sub <- c("LEFT CAROTID STENOSIS: (50-69)APPROXIMATELY 50-55% (0-49)LESS THAN 50%     COMMON:", "LEFT CAROTID STENOSIS: (50-69)APPROXIMATELY 60-70% (0-49)LESS THAN 50% COMMON:", "LEFT CAROTID STENOSIS: (40-60)APPROXIMATELY 40% INCOMPLETE SCAN SEE NOTES (40-50)LESS THAN 50% COMMON:")

d1$sub
[1] "LEFT CAROTID STENOSIS: (50-69)APPROXIMATELY 50-55% (0-49)LESS THAN 50% COMMON:"                        
[2] "LEFT CAROTID STENOSIS: (50-69)APPROXIMATELY 60-70% (0-49)LESS THAN 50% COMMON:"                        
[3] "LEFT CAROTID STENOSIS: (40-60)APPROXIMATELY 40% INCOMPLETE SCAN SEE NOTES (40-    50)LESS THAN 50% COMMON:"

提取子1

d1$sub1 <- as.character(lapply((strapply(d1$sub,"((?<=LEFT CAROTID STENOSIS:).{5,}?(?=(\\(|COMMON)))", perl=TRUE)), unique))
d1$sub1
[1] " (50-69)APPROXIMATELY 50-55% "                       
[2] " (50-69)APPROXIMATELY 60-70% "                       
[3] " (40-60)APPROXIMATELY 40% INCOMPLETE SCAN SEE NOTES "

现在引用 sub1 从数据中获取 sub2

想要返回“(0-49)LESS THAN 50%”、“(0-49)LESS THAN 50%”和“(40-50)LESS THAN 50%”

d1$sub2 <- as.character(lapply((strapply(d1$sub,"((?<=\\d1$sub1).*?(?=COMMON))", perl=TRUE)), unique))
d1$sub2
[1] "NULL" "NULL" "NULL"

*下面的原始帖子**

我正在从文本报告中提取医疗信息,并尝试使用一个变量 ($sub1) 作为正则表达式的一部分来查找下一个变量 ($sub2)。

如何使用变量中的值来正则表达式下一个变量?

library(plyr)
library(tm)
library(stringr)
library(gsubfn)

#Dataset of velocities
d1 <- c("CCA: 135 cm/sec ICA: 50 cm/sec", "CCA: 150 cm/sec ICA: 75 cm/sec")
d1
[1] "CCA: 135 cm/sec ICA: 50 cm/sec" "CCA: 150 cm/sec ICA: 75 cm/sec"

#Lookahead to get sub1
d1$sub1 <- as.character(lapply((strapply(d1,"(.*?(?=ICA:))", perl=TRUE)), unique))
Warning message:
In d1$sub1 <- as.character(lapply((strapply(d1, "(.*?(?=ICA:))",  :
 Coercing LHS to a list
d1
[[1]]
[1] "CCA: 135 cm/sec ICA: 50 cm/sec"

[[2]]
[1] "CCA: 150 cm/sec ICA: 75 cm/sec"

$sub1
[1] "CCA: 135 cm/sec " "CCA: 150 cm/sec "

#Now reference sub1 to get sub2 - does not work?
#Want to return "ICA:50 cm/sec" and "ICA:75 cm/sec"
#Used paste(d1$sub1) to try getting the $sub1 variable into the regex, but doesn't work)
d1$sub2 <- as.character(lapply((strapply(d1,"((?<=paste(d1$sub1)).*?)", perl=TRUE)), unique))
d1$sub2
[1] "NULL" "NULL" "NULL"

文本具有结构,但在长度、内容等方面变化很大。定义第一个变量 ($sub1) 很容易,但使用它来定义第二个变量将是最精确的。

也许我应该强调文本是非常多变的——所以基于文本模式的简单正则表达式将不起作用。我需要使用第一个变量在文本中定位第二个变量。这是医疗信息,所以我不能发布实际数据。

4

3 回答 3

5

尝试使用该paste0()功能。这将把你所有的变量和你想使用的任何正则表达式放在一起。

grep(paste0("^.*", variable, ".*$"), d1)

如果您的变量可以具有 >1 元素collapse = "",您还可以添加参数paste0()

于 2014-05-01T05:03:28.197 回答
2

尝试这个:

> d1 <- c("CCA: 135 cm/sec ICA: 50 cm/sec", "CCA: 150 cm/sec ICA: 75 cm/sec")
> t(strapplyc(d1, "\\w+: \\S+ \\S+", simplify = TRUE))
     [,1]              [,2]            
[1,] "CCA: 135 cm/sec" "ICA: 50 cm/sec"
[2,] "CCA: 150 cm/sec" "ICA: 75 cm/sec"
于 2013-08-21T15:43:03.760 回答
0

需要转义各种字符才能在正则表达式中使用变量,但为什么不做更简单的事情呢?

sub('(.*)ICA.*', '\\1', d1)
#[1] "CCA: 135 cm/sec " "CCA: 150 cm/sec "
sub('.*(ICA.*)', '\\1', d1)
#[1] "ICA: 50 cm/sec" "ICA: 75 cm/sec"
于 2013-08-21T15:40:29.090 回答