#!/usr/bin/perl use Modern::Perl; my $a = 'catonecattwocatthreecatfourcat'; $a .= ' '; my @w = split /cat/, $a; say "$_ -> " . length($_) for @w[1..@w-2]; one -> 3 two -> 3 three -> 5 four -> 4