in reply to matching a defined lenght

It seems that people have given four main methods of 'matching' 30 characters:

substr $string, 0, 30; # will extract UP TO the first 30 characters of the string. $string =~ m/(.{30})/; # will extract the first 30 characters, if the string is AT LEAST 30 + characters long $string =~ m/.{30}/; # tests if the string is AT LEAST 30 characters long length($string) == 30; # tests if the string is 30 characters long.

the last two are different in that return true/false, without trying to extract the first 30 characters, but they'll give different results for greater than 30 characters ... the first two are different at less than 30 characters:

my @strings = ( join ('', 'a' .. 'z' ), join ('', 'a' .. 'z', 0 .. 3 ), join ('', 'a' .. 'z', 0 .. 9 ), ); foreach my $string (@strings) { print "\nstring : $string\n"; print 'length : ', length($string), "\n"; print 'substr : ', substr( $string, 0, 30 ), "\n"; print 'regex : ', ( $string =~ m/.{30}/ ), "\n"; print 'regex : ', ( $string =~ m/(.{30})/ ), "\n"; }

produces:

string : abcdefghijklmnopqrstuvwxyz length : 26 substr : abcdefghijklmnopqrstuvwxyz regex : regex : string : abcdefghijklmnopqrstuvwxyz0123 length : 30 substr : abcdefghijklmnopqrstuvwxyz0123 regex : 1 regex : abcdefghijklmnopqrstuvwxyz0123 string : abcdefghijklmnopqrstuvwxyz0123456789 length : 36 substr : abcdefghijklmnopqrstuvwxyz0123 regex : 1 regex : abcdefghijklmnopqrstuvwxyz0123