Retrieve a number after a specific line

I need to find the number after the line "Count of". There may be a space or character between the "Count of" line and the number. I have something that works on www.regex101.com but does not work with the stringr function 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

What I want to get:

[1] NA NA NA NA "5" "50" "10"
+4
source share
3 answers
str_extract(shopping_list, "(?i)(?<=count of\\D)\\d+")
# [1] NA   NA   NA   NA   "5"  "50" "10"

where (?i)makes the case insensitive \\Dmeans not a number, but ?<=a positive lookbehind.

+1
source
as.numeric(sub("(?i).*count of.*?(\\d+).*", "\\1", shopping_list))
[1] NA NA NA NA  5 50 10

Regular expression pattern:

  • (?i): ignore case
  • .*count of.*?: any character length up to "count of"
  • (\\d+): Capturing one or more digits
  • "\\1": return capture group

- ""coconut count of - 5", "count of".

0

Look ahead and see what you are looking for with this grep ...

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 )[0-9]*")
[1] NA   NA   NA   NA   "5"  "50" NA  
0
source

All Articles