$_ = 'foo('; $regex = 'FOO('; $modifiers = 'i'; # a bit of error checking never goes astray die "Illegal modifier!" unless $modifiers =~ m/^[ismxo]\z/; # escape everything except the essential regex char and \w \s # this is a security measure as discussed below $regex =~ s/([^\w\s\\|()[\]{}^\$*+?.])/\\$1/g; # generate the regex with error checking $regex = eval "qr/$regex/$modifiers"; die "Eval failed $@\n" if $@; # test it print "match" if /$regex/; #### $regex =~ s/([^\w\s\\|()[\]{}^\$*+?.])/\\$1/g;