Re: Regex lookahead problem

2005-01-14 Thread Dave Gray
On Fri, 14 Jan 2005 07:11:30 +, 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]
 




Re: Regex lookahead problem

2005-01-14 Thread Andrew Black
Can someone explain how lookaheads work and give an example of using one
Cheers
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Regex lookahead problem

2005-01-11 Thread Jenda Krynicky
From: Ramprasad A Padmanabhan <[EMAIL PROTECTED]>
> I have a string
> 
> $X='#SOME_STRING END';
> 
> I want to remove the begining '#' and ending 'END' but retain
> everything in between. 
> 
> 
> This way works fine
> $X=~s/^#(.*?) END$/$1/; # $X is now SOME_STRING
> 
> But this way does not 
> s/^#(?=.*) END$//;   ## $X is still #SOME_STRING
> END

Yes, because the regexp did not match. The lookahead doesn't move the 
pointer in the string, which means the regexp engine validated that 
it can match the .* after the #, and then tries to match ' END$' 
immediately after the #.

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Regex lookahead problem

2005-01-11 Thread Ramprasad A Padmanabhan
I have a string

$X='#SOME_STRING END';

I want to remove the begining '#' and ending 'END' but retain everything
in between. 


This way works fine
$X=~s/^#(.*?) END$/$1/; # $X is now SOME_STRING

But this way does not 
s/^#(?=.*) END$//;   ## $X is still #SOME_STRING END



Any pointers why the lookahead ?= is not working

Thanks
Ram



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]