Fix infinite loop when regexp ends on line

Fixes issue #696