#!/usr/bin/perl use warnings; use strict; sub regex_pos { my ($string) = @_; $string =~ /[^ \t]/g; return pos($string) - 1 } sub tr_pos { my ($string) = @_; (my $tr = $string) =~ tr/ \t/!!/c; return index $tr, '!' } for my $s (" \t x ", "\t\t\t\t\t\t \xff...\t ") { regex_pos($s) == tr_pos($s) or die; } use Benchmark qw{ cmpthese }; my $s = ' ' x 200 . "\t" x 200 . "\x01" . " " x 200; cmpthese(-3, { regex => sub { regex_pos($s) }, tr => sub { tr_pos($s) }, });