问题
From this string border-color:#002449;left:74.4%top;37%;
I would like to make the first percentage 74.4%
a variable called X
and the second percentage 37%
a variable called Y
.
I have tried to play around with this regex "^.*?(\\d+)%.*"
but this takes out the %
sign and only extracts the second 4
from 74.4
Any help will be appreciated. Please let me know if any further information is needed.
回答1:
s <- "border-color:#002449;left:74.4%top;37%;"
regmatches(s, gregexpr("\\d+(\\.\\d+){0,1}%", s))[[1]]
# [1] "74.4%" "37%"
or
library(stringr)
str_extract_all(s, "\\d+(\\.\\d+){0,1}%")[[1]]
# [1] "74.4%" "37%"
来源:https://stackoverflow.com/questions/35822899/how-to-str-extract-percentages-in-r