Reputation: 21
I am working R. I want to extract all numbers between the last blank space and a string pattern ("-APPLE") in a vector. The numbers can be of variable length.
test_string = c("ABC 2-APPLE", "123 25-APPLE", "DEF GHI 567-APPLE", "ORANGE")
Expected Result set should be a vector as in c(2, 25, 567, NA)
Upvotes: 0
Views: 197
Reputation: 68
you can use the "rebus" package, which is very user-friendly in creating the regex patterns you need.
library(rebus)
## adjust the lo and hi arguments of dgt() based on your text
rx <- lookbehind(SPACE) %R% dgt(1,5) %R% lookahead("-APPLE")
str_extract(test_string, rx)
Upvotes: 1
Reputation: 31087
See Regex group capture in R with multiple capture-groups for an example of using str_match()
, from the stringr
package.
In your case:
> test_string = c("ABC 2-APPLE", "123 25-APPLE", "DEF GHI 567-APPLE")
>
> library(stringr)
> x <- str_match(test_string, " ([0-9]+)-APPLE$")[,2]
> as.numeric(x)
[1] 2 25 567
Upvotes: 1