Slash Boxes
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)


Just another perl hacker somewhere near Disneyland

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

I also have some modules [] on CPAN [] some of which are marginally [] more [] useful [] 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.
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 [] 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 :)