use strict; use warnings; $\="\n"; my %sp= ( foo => 'foo\d+', bar => 'bar\S+?' ); my $regex='§foo§|§bar§'; # template $regex=~s/§(\w+)§/(?<$1>$sp{$1})/g; #print $regex; my $str=" foo15 "; if ($str =~ m/$regex/) { while ( my( $name, $pos) = each %+ ) { print "$sp{$name} matched" if $pos; } }