$string = '\u $'; #should become #'username $' #ideally it would be implimented w/ a loop/switch while (#some magic) { switch ($1) { case "\u" { # $1 = $ENV{USER} } ... } } # or maybe a call to a sub replace($str, "\u" => $ENV{USER}, # replace \u with USER env var "\f" => sub { 'foo' }, # replace \f with the result of sub "\r\{([^}]+)}" = > sub { reverse($_[1] }, # replace \r{...} with string in braces reversed "\(?:\d\d\d)" => sub { chr($_[1] } # replace 3 numbers with the corresponding character number };