2016-10-03 78 views
1

x是一個字符串:R:一切不是,這是一個正則表達式

x="alt=\"white\"/>" 

我想提取"white"在一個正則表達式中的R 我嘗試

gsub(pattern ="[(^[:alpha:])|(alt)]" ,replacement ="" ,x =x) 

但很明顯, 這是行不通的。有任何想法嗎?

+1

你['regex'ing HTML(http://stackoverflow.com/questions/1732348)?餿主意。 – MichaelChirico

回答

3

這是你在找什麼?

some_vector <- c("alt=\"white\"/>", "alt=\"black\"/>") 
colours <- gsub('(alt)="([^"]+)"', '\\1=""', some_vector) 
colours 
# [1] "alt=\"\"/>" "alt=\"\"/>" 

一般來說,你應該去找一些解析器來代替。

+0

好的謝謝你的回答。通過解析器,你的意思是像strsplit? –

1

試試這個,如果你有興趣在一些模式只引號內出現:

gsub(".*\"(.*)\".*", "\\1", x) 
#[1] "white"