#!/usr/bin/perl -w use strict; my @strings = ("CATINTHEHATWITHABAT", "WERWERWAT134wAt"); my $regex = '\wAT'; foreach my $string (@strings) { while ( $string =~ m/($regex)/gi) { printf "%s found at pos %2d in %s\n", $1, pos($string)-length($1)+1, $string; } } __END__ Prints: CAT found at pos 1 in CATINTHEHATWITHABAT HAT found at pos 9 in CATINTHEHATWITHABAT BAT found at pos 17 in CATINTHEHATWITHABAT WAT found at pos 7 in WERWERWAT134wAt wAt found at pos 13 in WERWERWAT134wAt #### #!/usr/bin/perl -w use strict; my @strings = ("CATINTHEHATWITHABAT", "WERWERWAT134wAtThatat", "WERWERWAT134wAtThattat"); my $regex = '\wAT'; foreach my $string (@strings) { while ( $string =~ m/($regex)/gi) { printf "%s found at pos %2d-%-2d in %s\n", $1, pos($string)-length($1)+1, pos($string), $string; } } __END__ Prints: CAT found at pos 1-3 in CATINTHEHATWITHABAT HAT found at pos 9-11 in CATINTHEHATWITHABAT BAT found at pos 17-19 in CATINTHEHATWITHABAT WAT found at pos 7-9 in WERWERWAT134wAtThatat wAt found at pos 13-15 in WERWERWAT134wAtThatat hat found at pos 17-19 in WERWERWAT134wAtThatat WAT found at pos 7-9 in WERWERWAT134wAtThattat wAt found at pos 13-15 in WERWERWAT134wAtThattat hat found at pos 17-19 in WERWERWAT134wAtThattat tat found at pos 20-22 in WERWERWAT134wAtThattat Note: ranges do not "overlap", see "...Thattat"