Magnetic extraction of runaway electrons from a tokamak plasma