我需要找到字符串“Count of”后面的数字。 “计数”字符串和数字之间可能有空格或符号。我有一些可以在 www.regex101.com 上运行的东西,但不能与 stringr str_extract
函数一起使用。
library(stringr)
shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2", "monkey coconut 3oz count of 5", "monkey coconut count of 50", "chicken Count Of-10")
str_extract(shopping_list, "count of ([\\d]+)")
[1] NA NA NA NA "count of 5" "count of 50" NA
我想要得到什么:
[1] NA NA NA NA "5" "50" "10"
最佳答案
str_extract(shopping_list, "(?i)(?<=count of\\D)\\d+")
# [1] NA NA NA NA "5" "50" "10"
哪里(?i)
使模式不区分大小写,\\D
表示不是数字,并且 ?<=
是积极的回顾。
关于regex - 提取特定字符串后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35947123/