(Obvious) regex (and programming) tip

runrig on 2007-03-23T00:20:34

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).*/;


I don't think it does what you think...

Yanick on 2007-03-23T16:42:35

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]/'

Re:I don't think your test does what you think...

runrig on 2007-03-23T17:02:04

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 :)

Re:I don't think your test does what you think...

Yanick on 2007-03-23T17:43:05

*blink*

*open shell*

*typety-type*

*stare*

*blush*

*sigh*

*put on dunce cap* ;-)