#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @res = ( qr/Title: (.*?), Author: (\w+) (\w+)$/, qr/Title: (.*?), Author: (\w+) (\w+) Publisher: (\w+)$/, qr/Title: (.*?), Author: (\w+) (\w+) Publisher: (\w+) Year: (\w+)$/, ); my @answers; while (my $line = ){ for my $re (@res){ my @results; if (@results = $line =~ /$re/){ push @answers, [@results]; } } } print Dumper \@answers; __DATA__ Title: The Moor's Last Sigh, Author: Salman Rushdie Title: The God of Small Things, Author: Arundhati Roy Title: one, Author: two three Publisher: four Title: five, Author: six seven Publisher: eight Year: nine #### $VAR1 = [ [ 'The Moor\'s Last Sigh', 'Salman', 'Rushdie' ], [ 'The God of Small Things', 'Arundhati', 'Roy' ], [ 'one', 'two', 'three', 'four' ], [ 'five', 'six', 'seven', 'eight', 'nine' ] ];