Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 提取接近百分号的速率_Regex_R - Fatal编程技术网

Regex 提取接近百分号的速率

Regex 提取接近百分号的速率,regex,r,Regex,R,有这样的向量字符 c("E_B_V_BNOW_UN_3%_01-02-2016", "E_B_V_XX_3%_20%_01-02-2016", "E_B_YY_25%_3%_20%_01-02-2016") 我想提取%附近的费率 我可以使用strsplit和gsub的组合来实现,但这是使用单个正则表达式实现的一种方法吗 预期输出为: list(c(3),c(3,20),c(25,3,20)) 使用stringr::str\u extract\u all s <- c

有这样的向量字符

  c("E_B_V_BNOW_UN_3%_01-02-2016",
    "E_B_V_XX_3%_20%_01-02-2016",
    "E_B_YY_25%_3%_20%_01-02-2016")
我想提取
%
附近的费率

我可以使用
strsplit
gsub
的组合来实现,但这是使用单个正则表达式实现的一种方法吗

预期输出为:

list(c(3),c(3,20),c(25,3,20))

使用
stringr::str\u extract\u all

s <- c("E_B_V_BNOW_UN_3%_01-02-2016",
      "E_B_V_XX_3%_20%_01-02-2016",
      "E_B_YY_25%_3%_20%_01-02-2016")

library("stringr")
str_extract_all(s, "\\d+%")

正如其他回答者所指出的那样。这使用了。

使用
stringr::str\u extract\u all

s <- c("E_B_V_BNOW_UN_3%_01-02-2016",
      "E_B_V_XX_3%_20%_01-02-2016",
      "E_B_YY_25%_3%_20%_01-02-2016")

library("stringr")
str_extract_all(s, "\\d+%")

正如其他回答者所指出的那样。这使用了一个.

@davidernburg right,经过编辑以去除百分比。请注意,
as.numeric
是返回数字(而不是字符)所必需的results@DavidArenburg右,编辑以去除百分比。另外请注意,
as.numeric
是返回写入的数字(非字符)结果所必需的,答案与预期输出(数字向量列表)不匹配“[0-9.]+(?=%)将允许十进制数值,“[0-9.E]+(?=%)将允许更大的范围,但以可能的错误为代价。我认为关于什么是安全的替代方案有很多线索。如前所述,答案与预期输出(数字向量列表)不匹配“[0-9.]+(?=%)将允许十进制数值,“[0-9.E]+(?=%)将允许更大的范围,但以可能的错误为代价。我认为关于什么是安全的替代方案有很多线索。
regmatches(s, gregexpr("\\d+(?=%)", s, perl = TRUE))
# [[1]]
# [1] "3"
# 
# [[2]]
# [1] "3"  "20"
# 
# [[3]]
# [1] "25" "3"  "20"