reGroup function to group IPs by a new prefix