[tpm] Regex question

Alex Beamish talexb at gmail.com
Tue Jul 8 08:28:23 PDT 2008


Hi Madi,

I would suggest

  my $foo="ABC-987-01";
  my ($bar) = $foo =~ m/^(.+)-\d\d$/;

This produces $bar with a value of 'ABC-987'.

Alex

On Tue, Jul 8, 2008 at 11:00 AM, Madison Kelly <linux at alteeve.com> wrote:

> Hi all,
>
>  I've got a simple problem I often come across, and I've got a way to make
> it work. However, I've always felt there must be a more ... elegant way of
> doing it.
>
> For example, let's say I want to copy a variable and strip a bit off the
> end;
>
> my $foo="ABC-987-01";
> my $bar=$foo;
> $bar=~s/(\w+-\d+)-\d+/$1/;
> # $bar now 'ABC-987'.
>
> That's three lines. Is there a way to do this in one line? Specifically, is
> there a way to assign '$1' to a new variable in one go?
>
> Thanks!
>
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>



-- 
Alex Beamish
Toronto, Ontario
aka talexb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20080708/276749b7/attachment.html>


More information about the toronto-pm mailing list