If you can use the latest version of Perl, see this notation (?<name>...) in regexp perlre docs. This is more understandable if you use $ 1, $ 2, $ 3, etc.
SCRIPT
use v5.14; use Data::Dumper; my @inputs = ( 'INFO: Vikram 32 2012', 'SAL: 12000$','ADDRESS: 54, junk, JUNK' ); my %matching_hash= ( qr/^INFO:\s*(?<name>\S+)\s+(?<age>\S+)\s+(?<joining>\S+)/ => [ 'name', 'age', 'joining' ], qr/^SAL:\s*(?<salary>\S+)/ => [ 'salary' ], qr/ADDRESS:\s*(?<address>.*)/ => [ 'address' ], ); my %detail; while (my ($regex, $array) = each %matching_hash ) { INPUT: foreach my $input ( @inputs ) { next INPUT if not $input =~ m{$regex}; for my $name ( @$array ) { $detail{$name} = $+{$name}; } } } say Dumper( \%detail);
OUTPUT
$VAR1 = { 'name' => 'Vikram', 'address' => '54, junk, JUNK', 'age' => '32', 'joining' => '2012', 'salary' => '12000$' };
source share