1

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?

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
hans glick
  • 2,431
  • 4
  • 25
  • 40

2 Answers2

3

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.

Jan
  • 42,290
  • 8
  • 54
  • 79
1

Try this if you are interested in some pattern appearing within the quotes only:

gsub(".*\"(.*)\".*", "\\1", x)
#[1] "white"
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63