On Fri, 14 Jan 2005 07:11:30 +0000, Andrew Black
<[EMAIL PROTECTED]> wrote:
> Can someone explain how lookaheads work and give an example of using one
> Cheers

I find the full names of these regex constructs to be quite
enlightening: "zero-width (positive|negative) look-(ahead|behind)
assertion".

So for example, one way to "commify" numbers is:

__CODE__
#!/usr/bin/perl
use strict;
use warnings;

my @numbers = (100, 1000, 2536);

for my $number (@numbers) {
  my $commified = commify($number);
  print "before:\t$number\nafter:\t$commified\n---\n";
}

sub commify {
  my ($num) = @_;
  $num = reverse $num;
  $num =~ s/(\d{3}) # match 3 numbers
            (?!=,)  # not followed by a comma
            (?=\d)  # followed by another digit
           /$1,/gx; # stick a comma after what we matched
  return reverse $num;
}
__END__

HTH,
Dave

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to