recursive implementation of RE expression matching from the back