perl -e '""=~/(?{a})(?{$^R.b})?((?{ $^R.c})|(?{ $^R.d}))(?{print "$^R\n"})(?=a)(?!a)/' # output: abc abd ac ad