mochi
mochi

Reputation: 151

Conditional regular expressions in stringr

I'm wondering how to implement a conditional regular expression in R. It seems that this can be implemented in PERL:

?(if)then|else

However, I'm having trouble figuring out how to implement this in R. As a simple example, let's say I have the following strings:

c('abcabd', 'abcabe')

I would like the regular expression to match "bd" if it is there and "bc" otherwise, then replace it with "zz". Thus, I would like the strings above to be:

c('abcazz', 'azzabe')

I have tried this using both sub and str_replace neither of which seem to work. It seems that my syntax might be wrong in sub:

sub('b(?(?=d)d|c)', 'zz', c('abcabe','abcabd'), perl=TRUE)
[1] "azzabe" "azzabd"

The logic is "match b, if followed by d match d, otherwise match c". With str_replace, I get errors :

str_replace(c('abcabe','abcabd'), regex('b(?(?=d)d|c)'), 'zz')
Error in stri_replace_first_regex(string, pattern, fix_replacement(replacement),  : 
Use of regexp feature that is not yet implemented. (U_REGEX_UNIMPLEMENTED)

I primarily use stringr so would prefer a solution using str_replace but open to solutions using sub.

Upvotes: 2

Views: 1709

Answers (1)

revo
revo

Reputation: 48751

You are almost near but you should have conditional pattern true assertion in each step:

(?(?=.*bd)bd|bc)

Live demo

You don't even need conditional regex:

^(.*)bd|bc

R code:

sub('^(.*)bd|bc', '\\1zz', c('abcabe','abcabd'))

Upvotes: 3

Related Questions