perl_lookahead_examples

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



sfpug@sf.pm.org

Subject: [sf-perl] Re: Perl Cookbook commify recipe
From: Anirvan Chatterjee <x.sfpug@chatterjee.net>
Date: Wed, 13 Sep 2000 15:05:49 -0700

On Wed, Sep 13, 2000 at 02:20:52PM -0500, Robert Seymour wrote:
> I am relatively new to Perl.
> Per below from Perl Cookbook/www/perl.com works great.
> But - I do not exactly understand what the bold type expressions are doing.
> I looked in several Perl books to no avail.
> When convenient would someone please explain it to me at a "beginner's" level?

Sure. Again, here's the commify function:

> sub commify {
>     my $text = reverse $_[0];
>     $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
>     return scalar reverse $text;
> }

So first, we reverse the string.

Let's say the input is "1234567.890".

The reverse of that becomes "098.7654321".

Our string then gets run through:

    $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;

We're looking for groups of three digits "(\d\d\d)" followed by
another digit "(?=\d)" (using lookahead) so we can add commas in
the right place.

(We'll ignore the "(?!\d*\.)" for now.)

First match:

   098.7654321 becomes 098.765,4321
       ^^^                 ^^^^
Second match:

   098.765,4321 becomes 098.765,432,1
           ^^^                  ^^^^

So if we reverse that, we end up with "1,234,567.890"

Makes sense?

Going back to the "(?!\d*\.)" negative lookahead, say we're commifying
"1.23456".

First, we reverse that to "65432.1"

If we weren't aware of the decimal, we'd have commified it to
"65,432.1" which (reversed) ends up "1.23,456" (not what we want).

The "(?!\d*\.)" positive lookahead ensures that we commify blocks of
numbers only when they're not followed by more numbers, ending in a
period.

So when we run the substitution on "65432.1", it won't match anywhere,
and the string will be left as is.

The positive "(?=\d)" and negative "(?!\d*\.)" lookahead assertions
are the interesting part of the regular expression.

Resources:

Mark Kvale's Perl regex tutorial deals with lookahead and other
topics. It's online at <http://keck.ucsf.edu/~kvale/perlretut.pod> (or
in PDF at <http://mitglied.tripod.de/~mglinski/DL/perlretut.pdf>).

Jeffrey Friedl's _Mastering Regular Expressions_ is sometimes dense,
but highly recommended. <http://www.oreilly.com/catalog/regex/>

===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu