hans glick hans glick - 2 months ago 10
R Question

R : everything that is not AND that is in one regular expression

x is a string :

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


I want to extract "white" in one regex in R
I try

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


But obviously, it does not work. Any ideas?

Jan Jan
Answer

Is this what you're looking for?

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

Generally, you should go for some parser instead.