use strict; use warnings; # For example, get regexp from command line: $regepx = $ARGV[0]; # Look for and remove end slashes and modifiers. $regexp =~ s{^/} {} or die("Bad input\n"); $regexp =~ s{/([msix]*)$}{} or die("Bad input\n"); my $modifiers_on = $1; my $modifiers_off = join '', grep { index($modifiers_on, $_) < 0 } qw( x i s m ); # Add the modifiers: $regexp = "(?${modifiers_on}-${modifiers_off}:${regexp})"; # Compile the regexp and check for errors: $regexp = eval { qr/$regexp/ } or die("Bad regexp: $@\n"); # Now you can use it: print($str =~ $regexp ? 'match' : 'no match', "\n");