$date = "10-09-2004"; print &replace($date, q"(\d+)-(\d+)-(\d+)", q"$3-$2-$1"), "\n"; sub replace { my $value = shift; my $this = shift; my $with = shift; my $modifiers = shift; $this = &pattern($this, $modifiers); if ($modifiers =~ /g/) { $value =~ s/$this/qq{qq{$with}}/gee; } else { $value =~ s/$this/qq{qq{$with}}/ee; } return $value; } sub pattern { my $pattern = shift; my $modifiers = shift; if ($modifiers =~ /[^gism]/) { die "Only m, i, s and g are valid pattern modifiers."; } $modifiers =~ s/g//; return qr"(?$modifiers)$pattern"; }