[TriLUG] YAPQ (yet another perl question)..

Greg Brown gwbrown1 at gmail.com
Thu Jun 11 18:29:27 EDT 2009


You are all awesome and are on my Christmas list.  But I'm broke so I'll
have to send you broken computer parts.  Sorry about that.

But honestly, thanks for the help!!  That part of the script is working and
now it's on to the next thing I'd like to do.

TriLUG rocks!

On Thu, Jun 11, 2009 at 6:05 PM, Kevin Hunter <hunteke at earlham.edu> wrote:

> At 3:24pm -0400 on Thu, 11 Jun 2009, Greg Brown wrote:
> > so close:
> >
> > 15998976 bytes total (8083968 bytes free)
> > routername#Total bytes: 15998976
> > Free bytes:  8
> >
> > so it's picking up the total bytes but on the free bytes it's only
> picking
> > up the last digit of the total free.
>
> This is because operators, for performance reasons, are "greedy" unless
> told otherwise.  So, the '.*' is matching as much as possible before it
> lets the [0123456789]+ match.  To correct it, I'd suggest being 100%
> explicit in what you want:
>
> >> $string =~ m/([0123456789]+) bytes total.*([0123456789]+) bytes free/;
>
> $string =~ m/^(\d+) bytes total \((\d+) bytes free/;
>   or
> $string =~ m/^(\d+) bytes total.*?(\d+) bytes free/;
>
> ^ - Make sure to start the match at the beginning of the line
> \d - shorthand for [0123456789]
> * - match preceding character as many times as possible
> *? - match preceding character as few times as possible
>
> I'd suggest the first form, as it's more explicit.  Most folks can't
> read regular expressions, so wherever possible, I suggest you make them
> as simple *and explicit* as you can.
>
> Cheers,
>
> Kevin
> --
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG FAQ  : http://www.trilug.org/wiki/Frequently_Asked_Questions
>



More information about the TriLUG mailing list