Handle nils and avoid incompatible encoding regexp error