简短地问,想要抓取的字符串前后满足一定的正则,但其本身并没有特殊规律,应如何实现?
具体:
例如一堆字符串:
a<-c("http://asdfghjk.com","asdfhttp://asdf.com","qwerhttp://poiu.org","dfasdfhttp://qwer.comqwer")
利用下面谷歌找到的代码
GetContent <- function(list,pattern){
d <- c()
for (i in 1:length(list)) {
g <- gregexpr(pattern,list)[[1]]
d <- append(d,substring(list,g,g+attr(g,'match.length')-1))
}
d
}
pattern <- "http://.*?(\\.com|\\.org)"
GetContent(a,pattern)
抓取到c("http://asdfghjk.com","http://asdf.com","http://poiu.org","http://qwer.com")
但实际上我只想要中间的,就是c("asdfghjk","asdf","poiu","qwer"),应该如何解决?