简体   繁体   English

如何在正则表达式中跟随某些单位的数字?

[英]How to the numbers that are followed by certain units in regular expression?

Extract the numbers (return only integers) that are followed by the units 'ml' or 'lb' in the following text:提取以下文本中后跟单位“ml”或“lb”的数字(仅返回整数):

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

Could anyone help?有人可以帮忙吗?

We can use str_extract with regex lookaround我们可以使用str_extract和 regex str_extract

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM