in reply to Executing a "/foo/flags" regex from a string

Here is a small snippet that should do what you were asking for (more or less):

perl -E 'my $str = "/regex/i"; my ($pat, $flags) = $str =~ m{^/(.+)/([imsx]*)\z} ? ($1, $2) : die "Bad regex"; my $qr = qr/(?$flags:$pat)/; say qq{Matched "$_" with $qr} for grep {$_ =~ $qr} qw(foo bar baz reGex)' Matched "reGex" with (?^u:(?i:regex))


my @a=qw(random brilliant braindead); print $a[rand(@a)];

Replies are listed 'Best First'.
Re^2: Executing a "/foo/flags" regex from a string
by Radiola (Monk) on Dec 30, 2015 at 18:05 UTC

    Ah, OK. I guess there's nothing more to the syntax of /regex/flags than, uh, the regex and the flags, so you don't risk losing anything by parcelling them out and putting them back together like that.

    Thanks!

    - Aaron