Reputation: 106
I have a strings that look like that:
strings <- c("100% 16 Feedback Kakkanad, Ernakulam","98% 159 Feedback Greater Kailash Part 1, Delhi","Bannerghatta Road, Bangalore ₹250 Available on Su")
I want to extract number of feedbacks using stringr. After some tries I almost got it.
stringr
str_extract_all(strings,"\\%.*[0-9]\\s", simplify = T)
Function above returns vector:
"% 16 ", "% 159 ",""
I would like to extract only this number, so remove % and white spaces. I tried to search, but I didn't know how to define my problem in fewer words :(
Upvotes: 1
Views: 365
Reputation: 887118
We can use str_extract
with a regex lookaround to extract the digits that follow any zero or more spaces (\\s*
) followed by the string 'Feedback'
library(stringr)
as.numeric(c(str_extract_all(strings, "\\d+(?=\\s*Feedback)", simplify = TRUE)))
#[1] 16 159
The issue with matching the %
is that it also become part of the output. Instead, it can be a regex lookaround
as.numeric(str_extract_all(strings,"(?<=\\%\\s)[0-9]+", simplify = TRUE))
#[1] 16 159
Upvotes: 1