#!/usr/bin/perl use strict; use warnings; my $timeformat = "*%H:%M:%S%% >"; # Example. my %replacements = ( '%' => '\%', 'a' => '[[:alpha:]]+', 'H' => '\d{2}', 'M' => '\d{2}', 'S' => '\d{2}' ); $timeformat = quotemeta($timeformat); $timeformat =~ s/\\\%\\?(.)/$replacements{$1}/eg; print ("The regular expression is: $timeformat\n");