I have a column with value as
\"RED LOBSTER CA04606\" or \"Red Lobster NewYork WY245\" n so on
How can I extract just the name Red Lobster or Red Lobster N
Since you're trying to use stringr, I recommend str_extract (I'd recommend it even if you weren't trying to use stringr):
x <- c('RED LOBTSER CA04606', 'Red Lobster NewYork WY245')
str_extract(x, '[a-zA-Z ]+\\b')
# [1] "RED LOBSTER " "Red Lobster NewYork "
The '\b' in the regex prevents the 'CA' from 'CA04606' being extracted.
If you don't like that trailing space you could use str_trim to remove it, or you could modify the regex:
str_extract(x, '[a-zA-Z]+(?: +[a-zA-Z]+)*\\b')
# [1] "RED LOBSTER" "Red Lobster NewYork"
Note - if your string has non-numbers after the post code, the above only returns the words before. So in the example below, if you wanted to get the 'NewYork' after the 'WY245', you can use str_extract_all and paste the results together:
x <- c(x, 'Red Lobster WY245 NewYork')
str_extract_all(x, '[a-zA-Z]+(?: +[a-zA-Z]+)*\\b')
# [[1]]
# [1] "RED LOBSTER"
#
# [[2]]
# [1] "Red Lobster NewYork"
#
# [[3]]
# [1] "Red Lobster" "NewYork"
# Paste the bits together with paste(..., collapse=' ')
sapply(str_extract_all(x, '[a-zA-Z]+(?: +[a-zA-Z]+)*\\b'), paste, collapse=' ')
# [1] "RED LOBSTER" "Red Lobster NewYork" "Red Lobster NewYork"