$text = "1981 born; 1993 7th grade; 1995 9th grade; 1999 RPI; 2001 TPC"; @section = ('\d{4}', '([^;]+)'); $text =~ m{ (?{ @s = map [], 1 .. @section }) (?: (?{ local $s[0] = [ @{ $s[0] }, [ length "$`$&" ] ] }) \d{4} (?{ $s[0][-1][1] = length "$`$&" }) \s+ (?{ local $s[1] = [ @{ $s[1] }, [ length "$`$&" ] ] }) [^;]+ (?{ $s[1][-1][1] = length "$`$&" }) (?: ;\s+ | $ ) )+ (?{ @watch = @s }) }x; for (0 .. $#watch) { print "$section[$_] matched in the following places:\n"; while (my $find = shift @{ $watch[$_] }) { my ($s, $e) = @$find; print substr($text, 0, $s), "<\e[1m", substr($text, $s, $e-$s), "\e[m>", substr($text, $e), "\n"; } }