特定字符串后提取数字

Mat*_*ews 5 regex r stringr

我需要在字符串"Count of"之后找到数字."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
Run Code Online (Sandbox Code Playgroud)

我想得到什么:

[1] NA NA NA NA "5" "50" "10"
Run Code Online (Sandbox Code Playgroud)

Jul*_*ora 6

str_extract(shopping_list, "(?i)(?<=count of\\D)\\d+")
# [1] NA   NA   NA   NA   "5"  "50" "10"
Run Code Online (Sandbox Code Playgroud)

where(?i)使模式不区分大小写,\\D意味着不是数字,并且?<=是积极的后向查找。