#! /usr/bin/perl use strict; use warnings; my %hash = ( foo1 => 'test', foo2 => 'test2', ); for my $k (keys %hash) { next unless( my ($id) = $k =~ /^foo(\d+)$/ ); # $id entspricht $1 des regex matchings print $id, "\n"; }