while( $string1 =~ s/B-/BB/ or $string1 =~ s/-B/BB/ ) { # Replacement already happened in the while condition };