Here's a solution:
s <- "Offer Disposition. MSISDN: 7183067962. Offer: . Disposition: DECLINED. Reason: Not interested. ChannelID: CARE."
sub(".*Reason: (.*?)\\..*", "\\1", s)
# [1] "Not interested"
Update (to address comments):
If you also have strings that do not match the pattern, I recommend using regexpr
instead of sub
:
s2 <- c("no match example",
"Offer Disposition. MSISDN: 7183067962. Offer: . Disposition: DECLINED. Reason: Not interested. ChannelID: CARE.")
match <- regexpr("(?<=Reason: ).*?(?=\\.)", s2, perl = TRUE)
ifelse(match == -1, NA, regmatches(s2, match))
# [1] NA "Not interested. ChannelID: CARE"
For you second example, you can use the following regex:
s3 <- "Delete Payment Arrangement of type Proof of Payment for BAN : 907295267 on date 02/01/2014, from reason PAERR."
# a)
sub(".*type (.*?) for.*", "\\1", s3)
# [1] "Proof of Payment"
# b)
match <- regexpr("(?<=type ).*?(?= for)", s3, perl = TRUE)
ifelse(match == -1, NA, regmatches(s3, match))
# [1] "Proof of Payment"