R,regexpr,gregexpr,跟踪比赛

时间:2015-03-05 14:29:36

标签: regex r

我们说我有这个data.frame,并希望将A列与下面的模式相匹配。这可以使用regexpr或gregexpr来完成。然而,我想跟踪匹配的行以及匹配本身。

df <- data.frame(A=c("where is the pencil? ","the white cat in the kitchen","green hat is over the blue ocean"))

> df
##                                  A
## 1            where is the pencil? 
## 2     the white cat in the kitchen
## 3 green hat is over the blue ocean

pattern <- ("(blue|white|green) \\w*")

regmatches(df[,1],regexpr(pattern,df[,1],perl=TRUE))

> regmatches(df[,1],regexpr(pattern,df[,1],perl=TRUE))
## [1] "white cat" "green hat"

期望的输出:

##                                  A     match
## 1            where is the pencil?       <NA>
## 2     the white cat in the kitchen white cat
## 3 green hat is over the blue ocean green hat

1 个答案:

答案 0 :(得分:1)

pattern更改为:

pattern <- paste0(pattern, "|$")

然后用NA替换空字符串。