use strict; use warnings; ## some sample 'types' and potential values my %types = ( name => [ 'George Bush', 'Osama Bin Laden', ], country => [ 'America', 'Your Pants', ], ); ## turn those arrays into foo|bar|baz (quotemeta to deal with potential U.S.A.) my %regexes = map { $_ => join( '|', map quotemeta, @{ $types{$_} }) } keys %types; ## some sample sentences my @sentences = ( 'my name is George Bush and I live in America', 'my name is Osama Bin Laden and I want to visit Your Pants', ); ## discover 'types' in each sentence foreach my $sentence ( @sentences ) { print "before: $sentence\n"; while ( my ($type, $pattern) = each %regexes ) { $sentence =~ s/$pattern/<$type>/g; } print " after: $sentence\n\n"; }