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
  • 19,788
  • 2
  • 37
  • 54
hans glick
  • 2,133
  • 3
  • 22
  • 38

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
  • 40,932
  • 8
  • 45
  • 77
1

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

gsub(".*\"(.*)\".*", "\\1", x)
#[1] "white"
Sandipan Dey
  • 19,788
  • 2
  • 37
  • 54