core: fix infinite loop when a regex gives an empty match (bug #38112)