I'm trying to teach myself how to use Regexp::Common, and I'm having a little trouble.
The following works as expected, and finds the number 1234 embedded in the string aaaa1234cccc:
use strict;
use Regexp::Common;
while ( my $word = <DATA> ) {
chomp $word;
if ( $word =~ /$RE{num}{int}/ ) {
print "Integer detected: \"$word\"\n";
} else {
print "$word\n";
}
}
__DATA__
aaaabbbbcccc
aaaa1234cccc
ddddeeeeffff
However, this does NOT work as I would expect:
use strict;
use Regexp::Common;
while ( my $word = <DATA> ) {
chomp $word;
if ( $word =~ /$RE{profanity}/ ) {
print "Profanity detected: \"$word\"\n";
} else {
print "$word\n";
}
}
__DATA__
aaaabbbbcccc
aaaaXXXXcccc
ddddeeeeffff
In this case, change XXXX into your favorite 4 letter offensive word. If I change the data string to this: "aaaa XXXX cccc" (i.e., add spaces around the XXXX, then it finds it).
It seems like the profanity patterns have start of word / end of word anchors built into the patterns, and thus don't work if the word is embedded inside another string? Is there any way to control this behavior? I've gone through the docs, but so far I can't find a way.
I'm using perl 5.14 (activestate) on Win7.
Thanks for any push in the right direction.
-
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.