Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

runrig (3385)

runrig
  dougwNO@SPAMcpan.org

Just another perl hacker somewhere near Disneyland

I have this homenode [perlmonks.org] of little consequence on Perl Monks [perlmonks.org] that you probably have no interest in whatsoever.

I also have some modules [cpan.org] on CPAN [cpan.org] some of which are marginally [cpan.org] more [cpan.org] useful [cpan.org] than others.

Journal of runrig (3385)

Thursday March 22, 2007
07:20 PM

(Obvious) regex (and programming) tip

[ #32773 ]
Avoid repeating yourself. I had several regexes using the 's' modifier, yet still wanted to match optional contiguous non-newline whitespace in many spots. So I used [^\S\n]*. Except that in several places, I had [\S\n]*. Oops (it would have been slightly more obvious if I had also taken more advantage of the 'x'modifier). Solution:

my $WS = qr/[^\S\n]*/;
...
my $regex = qr/blah,blah,blah,${WS},blah,blah/;

or perhaps this would be better (or just as well):

my $WS = qr/(?m).*/;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • My coffee-gauge is in the red at the moment, so I'm probably overlooking something, but I don't think

    my $WS = qr/[^\S\n]*/;

    is doing what you think it does. '\S' within a character set doesn't expand to 'all non-space characters', but only means a regular, boring 'S'. E.g.:

    $ perl -e'print "See" =~ /[\S]/'
    • A quick check of perlre [perl.org] shows that \S does match all non-whitespace characters, and since your test consists of non-whitespace (including an 'S' which would have matched even if \S did what you thought it would do), your regex matches. Leave the "S" out of your test string and it will still match...then set the test string to a single "\t" and it won't match :)