You mean you start with /test/smi from some outside source? The only way to use that is to eval it. Of course, that means using eval on something from an outside source, which is a big no-no.
If the user can only use / as the delimiter, then a simple regexp will process the options:
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");
It's trickier if you want to support substitutions and the (g)lobal modifier. You're better off asking for these as seperate arguments. (The search string, the replace string and the modifiers.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|