use strict; use warnings; use Benchmark qw( cmpthese ); my @data = ; sub test_m { my @rv; foreach (@data) { local $_ = $_; # while () my ($host, $test) = / ( # Start first capture [\w\-]+ # One or more alphanum or hyphens (?: # non-capturing lookahead ,my-domain,com # Literal string )? # Make it optional ) # End of first capture (?: # non-capturing lookahead [\w\-,]+ # One or more alpanum or hyphens )? # Make it optional \. # A literal period ( # Start second capture [a-z]+ # One or more lowercase chars ) # End second capture /x or next; $host =~ s/,/./g; push(@rv, "Host:$host Test:$test\n"); } @rv; } sub test_i { my @rv; foreach (@data) { local $_ = $_; # while () chomp; my ($host, $test) = split(/\./, $_, 2); $host =~ s/,/./g; $host =~ s/\.my-domain\.net$//; push(@rv, "Host:$host Test:$test\n"); } @rv; } sub test_i2 { my @rv; foreach (@data) { local $_ = $_; # while () my ($host, $test) = /([^,.]+(?:,my-domain,com)?)[^.]*\.(.+)/x or next; $host =~ s/,/./g; push(@rv, "Host:$host Test:$test\n"); } @rv; } print("m:\n"); print test_m(); print("--\n"); print("i:\n"); print test_i(); print("--\n"); print("i2:\n"); print test_i2(); cmpthese(-2, { m => \&test_m, i => \&test_i, i2 => \&test_i2, }); __DATA__ hosta-sel-kr-1,my-domain,net.testa hostb-sel-kr-1,my-domain,net.testb hostc-sel-kr-1,my-domain,com.testa hostd-sel-kr-1,my-domain,com.testc hoste-sel-kr-1,my-domain,net.testxyz hosta-mel-au-1,my-domain,net.testabc hosta-mel-au-1,my-domain,net.testdef hostxyz.testabc someotherhost.someothertest