foreach $string ( "s005219/Doe John|John.Doe\\", "s0052194/Doe Thomas|Thomas.Doe\\", ) { ($reversed_name) = $string =~ m!/(.*?)\|!; print($reversed_name, "\n"); } # Prints # ====== # Doe John # Doe Thomas #### foreach $string ( "s005219/Doe John|John.Doe\\", "s0052194/Doe Thomas|Thomas.Doe\\", ) { if (m!/(.*?)\|!) { print("$1\n"); } }