I wouldn't consider myself a Perl expert, so I won't comment on the others' replies, which certainly are correct. However, Perl (the newer versions anyways) supports dynamic RegEx creation, so you might want consider using the following to get the desired results (it does seem to work ;-):