Fix stack overflow in regex