Regular expression in R


#1

I have a string (example below):
"INFO\tC:98.0%[S:96.6%,D:1.4%],F:0.7%,M:1.3%,n:148"

I would like to split this string and extract the numbers corresponding to S,D, F and M as four different variables. I should get like this:
96.6
1.4
0.7
1.3


#2

Something like this?

library(stringr)
x = "INFO\tC:98.0%[S:96.6%,D:1.4%],F:0.7%,M:1.3%,n:148"

as.numeric(str_match_all(x, "[SDFM]:([0-9]+?\\.[0-9]+?)")[[1]][, 2])
# > [1] 96.6  1.4  0.7  1.3

#3

Yes, that's what I wanted. Thank you!


#4

If you find yourself doing more of this sort of thing, regexplain is super helpful!