#!/usr/bin/perl use warnings; use strict; my $input = 'tar : ERGC [1] | ALX5 [2] | PT2 [3] | PRTD [4] act: Ago [1] : Inhibit [2,3,4]'; my ($left, $right) = split /act:/, $input; my %num2left; while ($left =~ /(\S+) \[([0-9])\]/g) { $num2left{$2} = $1; } while ($right =~ /(\S+) \[([0-9,]+)\]/g) { my ($target, @indices) = ($1, split /,/, $2); for my $i (@indices) { print "$i $num2left{$i}\t$target\n"; } }