R 如何计算正则表达式中后跟某些单位的数字?

R 如何计算正则表达式中后跟某些单位的数字?,r,R,提取以下文本中后跟单位“ml”或“lb”的数字(仅返回整数): myText <- 'At 10am we had 40ml of water, 5 lb of meat, 3lb of salt and 20 apples.' myText我们可以将str\u extract与regex lookaround一起使用 library(stringr) as.numeric(str_extract_all(myText, "\\d+(?=\\s*(ml|lb))")[[1]]) #[1

提取以下文本中后跟单位“ml”或“lb”的数字(仅返回整数):

myText <- 'At 10am we had 40ml of water, 5 lb of meat, 3lb of salt and 
20 apples.'

myText我们可以将
str\u extract
与regex lookaround一起使用

library(stringr)
as.numeric(str_extract_all(myText, "\\d+(?=\\s*(ml|lb))")[[1]])
#[1] 40  5  3

非常感谢。有帮助