2017-10-06 35 views
-1

我想創建一個函數從我的腳本中提取包名。到目前爲止,我已經能夠提取所有使用libraryrequire加載的軟件包。我還注意到,有時我使用的功能是使用::操作員。對於這些情況,我有以下情況但不起作用:在導出的函數中匹配包的正則表達式

a <- c(
    "ggplot2::aes()", 
    "      digest::digest(data))", 
    " data <- dplyr::rbind_all(data)" 
) 
gsub('^(.+)::(.+)', '\\1', a) 

在此示例中,我想獲得ggplot2, digest and dplyr作爲一個載體。

+0

讓你的輸出應該給你GGPLOT2,消化和dplyr的答案嗎? –

+0

''^(。+)::(。+)''這裏的第一個捕獲組匹配字符串中從行開始到最後一個'::'的任何字符。正則表達式正在按照我預期的方式工作。比賽的條件是什麼? – Jota

回答

0

您可以使用str_extract

out <- str_extract("data <- dplyr::rbind_all(data)", regex("[a-zA-Z]+::")) 
> substr(out, 1, (nchar(out)-2)) 
[1] "dplyr" 

> out <- str_extract("digest::digest(data))", regex("[a-zA-Z]+::")) 
> substr(out, 1, (nchar(out)-2)) 
[1] "digest" 
> 
1

對於從字符串,regmatches包名的提取,使用regexec功能。

第一步:提取的條件之前,::

s1 <- regmatches(a, regexec("^.*?::",a)) 
> s1 
[[1]] 
[1] "ggplot2::" 

[[2]] 
[1] "      digest::" 

[[3]] 
[1] " data <- dplyr::" 

說明

^asserts position at start of a line 
    .*? matches any character (except for line terminators) 
    *? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy) 
    :: matches the characters :: literally (case sensitive) 

第二步:更換::,空格和長期<-之前,

s2 <- gsub(".*(<-)|::| ","",s1) 
> s2 
[1] "ggplot2" "digest" "dplyr" 
0

你可以做這樣的事情:

## Your data: 
a <- c("ggplot2::aes()", 
     " data.table::dcast(data, time ~ variable, fun = mean)", 
     " data <- dplyr::rbind_all(data)") 

## Extract string that consists of letters, numbers or a dot 
## and has at least two characters (= package naming conventions), 
## and that is followed by "::" 
stringr::str_extract(a, "[[:alnum:].]{2,}(?=::)") 
# [1] "ggplot2" "data.table" "dplyr"