用于获取URL目录的自定义函数

时间:2016-07-05 13:31:33

标签: regex r strsplit

看起来很简单,

考虑以下网址

[1] "scripts.iucr.org/cgi-bin/paper?S1600536812045886"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
[2] "cpa-seoadvisors.com/cvv/auth/auth/view/pdf/index.html/"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
[3] "www.scirp.org/journal/PaperDownload.aspx?DOI=10.4236/csta.2012.13014"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
[4] "www.google.com.cy/search?q=DNS+traffic&es_..."                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
[5] "seesaa.net/pede/lobortis/ligula/sit/amet.png?semper=vitae&est=..."

我希望得到第一个'/'和将令牌与?分开的那个之间的部分。 我写了以下函数

get_directory <- function(x){
  dir <- sapply(strsplit(x, '/'), function(i)sum(grepl('\\?', i)))
  ifelse(dir > 0, sapply(strsplit(x, '/'), function(i) paste(i[-c(1, length(i))], collapse = '/')), 0)
}

但它在[3]和[4] URL失败了。

预期输出应为

"cgi-bin"
"0"
"journal"
"0"
"pede/lobortis/liguls/sit"

数据

dput(df)
structure(list(V1 = c("scripts.iucr.org/cgi-bin/paper?S1600536812045886", 
"cpa-seoadvisors.com/cvv/auth/auth/view/pdf/index.html/", "www.scirp.org/journal/PaperDownload.aspx?DOI=10.4236/csta.2012.13014", 
"www.google.com.cy/search?q=DNS+traffic&es_...", "seesaa.net/pede/lobortis/ligula/sit/amet.png?semper=vitae&est=..."
)), .Names = "V1", row.names = c(NA, -5L), class = "data.frame")

1 个答案:

答案 0 :(得分:4)

我们可以使用str_extract。使用正则表达式外观,我们匹配一个或多个字符(.*),其后成功/后跟/,一个或多个字符不是?({{ 1}})后跟[^?]+

?