[tpm] Regex question

Chris Jones cj at enersave.ca
Fri Oct 19 22:54:02 PDT 2012


Thanks Liam, we are getting closer but no cigar just yet:

#!/usr/local/bin/perl
use strict;
my $inLine = "Kitchen Area 1C Spc 
zn         2750.     1400.     0.249     1.000";

my @zoneLine = ($inLine =~ m{
        ^(.{25})         # the zone name
         \s*([\d.]+)     # supply flow
         \s+([\d.]+)     # exhaust flow
         \s+([\d.]+)     # fan kw
         \s*([\d.]+)     # minimum flow
         \s*
        $});


if (@zoneLine == 5) { # matched OK
     $zoneLine[0] =~ s/\s+$//; # remove trailing whitespace
} else {
     print "unmatched: $_\n";
}

Output: "unmatched"

At 12:02 AM 20/10/2012, Liam R E Quin wrote:
>[resending from the right account - I hate the Evolution mailer 
>sometimes. I should switch to Inteligent Design]
>
>On Fri, 2012-10-19 at 21:43 -0400, Chris Jones wrote:
>
> > Kitchen Area 1C Spc zn         2750.     1400.     0.249     1.000
> > Kitchen Area 1C Pl zn             0.        0.     0.000     0.000
>
> > my @zoneLine = split /[\w\s{0,25)$]/+\w{4}/, $inLine;
>
>This is muddled thinking. Take off your shoes, drink a cup of tea, and
>try again :-)
>
>Someone else suggested using unpack but I'd probably stick with regular
>expressions if you can take the time to get used to them, because
>they're really really useful an powerful once you have tamed them.
>
>[...] is a character class. A character class with one space in it
>matches the same as a character class with 25 spaces in it.
>[abc] matches an "a", a "b" or a "c" in the input.
>[a{0,25}] matches an "a", a "{", a "0", a ",", a "2", a "5" or a "}".
>
>[\w\s]{0,25} matches between 0 and 25 characters, each of which is a
>word or space character. It's also not what you want, but it's closer.
>
>Note also that split takes a regular expression that *separates* fields,
>and does not return the part of the input that matched the pattern.
>
>my @zoneLine = ($inLIne =~ m{
>        ^(.{25})         # the zone name
>         \s*([\d.]+)     # supply flow
>         \s+([\d.]+)     # exhaust flow
>         \s+([\d.]+)     # fan kw
>         \s*([\d.]+)     # minimum flow
>         \s*
>        $});
>
>if (@zoneLine = 5) { # matched OK
>     $zoneLine[0] =~ s/\s+$//; # remove trailing whitespace
>     etc_etc_go_crazy)(;
>} else {
>     print "unmatched: $_\n";
>}
>
>Liam
>
>--
>Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
>Pictures from old books: http://fromoldbooks.org/
>Ankh: irc.sorcery.net irc.gnome.org freenode/#xml
>
>--
>Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
>Pictures from old books: http://fromoldbooks.org/
>Ankh: irc.sorcery.net irc.gnome.org freenode/#xml

 >>
Christopher Jones, P.Eng.
Suite 1801, 1 Yonge Street
Toronto, ON M5E1W7
Tel. 416-203-7465
Fax. 416-946-1005
email cj at enersave.ca



More information about the toronto-pm mailing list