Remember to use quotemeta or \Q and \E on any variables
you are going to use inside a regex. If you forget to, and
any special regex meta-characters exist in your variables,
your program can do anything between match unwanted strings
to completely die'ing.
I see this problem in alot of code where the program "builds"
a regex. Bugs caused by forgetting to do this can go
undetected for a long time until a "+", "|" or a ")" occurs
somewhere in the incoming data. (assuming the data
being searched through is not hard coded into the script,
and thus it's content controlled by the programmer - as
it's often not in the real world)
Here's some sample code to illustrate the use of quotemeta
in solving your problem:
#!/usr/bin/perl -w
use strict;
use constant STRING => 'clintonesque';
use constant TO_MATCH => qw(
Clinton
Bush
Reagan
);
my $regex = join '|', map { quotemeta } TO_MATCH;
my ($first_match) = STRING =~ /($regex)/i;
print $first_match;
-
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.
|