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
In reply to Executing a "/foo/flags" regex from a string by Radiola
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |