本文介绍了如何str_extract R中的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个字符串border-color:#002449;left:74.4%top;37%;中,我想将第一个百分比74.4%称为变量X,将第二个百分比37%称为变量Y.

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.

我尝试使用此正则表达式"^.*?(\\d+)%.*",但这会除去%符号,仅从74.4中提取第二个4

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.

推荐答案

s <- "border-color:#002449;left:74.4%top;37%;"
regmatches(s, gregexpr("\\d+(\\.\\d+){0,1}%", s))[[1]]
# [1] "74.4%" "37%"

library(stringr)
str_extract_all(s, "\\d+(\\.\\d+){0,1}%")[[1]]
# [1] "74.4%" "37%"

这篇关于如何str_extract R中的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:55