Radiola has asked for the wisdom of the Perl Monks concerning the following question:
I wish to take a string like "/regex/i" (say, from the command line) and make a regex object with qr that I can execute just like I'd written that in the program text. I'm not sure about the best way to do it. I tried:
if ($search_term =~ m[^/.*/\w*$]) { # if it is in the form "/foo/x" or whatever, treat it as # though it had been specified in Perl program text $search_re = qr($search_term); }
If I input /tree/i, it returns a regex (?^:/tree/i) that searches for "/tree/i", which isn't what I want. I want something like (?^i:tree) which will match "tree" or "Tree" or "Street". That I can get using a string eval():
$search_re = eval("qr${search_term}");
Is there a better way to do this? This is a personal command-line tool, so I'm not really worried about malicious input to the string eval; but if there's a better way I'd like to know for the future.
Thanks,
Aaron
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Executing a "/foo/flags" regex from a string
by Rhandom (Curate) on Dec 30, 2015 at 01:59 UTC | |
by Radiola (Monk) on Dec 30, 2015 at 18:05 UTC | |
|
Re: Executing a "/foo/flags" regex from a string (qr flags)
by Anonymous Monk on Dec 30, 2015 at 01:52 UTC |