Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract number after specific string

Tags:

regex

r

stringr

I need to find the number after the string "Count of". There could be a space or a symbol between the "Count of" string and the number. I have something that works on www.regex101.com but does not work with stringr str_extract function.

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"
like image 712
Matthew Crews Avatar asked Mar 11 '16 18:03

Matthew Crews


2 Answers

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

where (?i) makes the pattern case insensitive, \\D means not a number, and ?<= is a positive lookbehind.

like image 168
Julius Vainora Avatar answered Oct 09 '22 00:10

Julius Vainora


Look ahead and look behinds are 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  
like image 34
cory Avatar answered Oct 09 '22 00:10

cory