[PLUG] Long Perl regex...

Jeme A Brelin jeme at brelin.net
Thu Sep 18 12:59:01 UTC 2003


On Thu, 18 Sep 2003, Matt Alexander wrote:
> I have 6 numbers that appear at the end of a line of text.  The 6 numbers
> may or may not have spaces between each.  So, for example, I could have:
>
> 1 234 5 6
> or
> 1234 56
>
> I constructed the following regex to strip out the spaces:
>
> $line =~
> s/(.*)([0-9])\s?([0-9])\s?([0-9])\s?([0-9])\s?([0-9])\s?([0-9])\s?$/$2$3$4$5$6$7/;
>
> This works, but it's long and I'm curious if there's a shorter, more
> clever way.

Is this always six numbers and always last?

Assume your sting is $line.

my $foo = $line;

$foo =~ s/\s//g;

$foo = substr($foo,-6,6);

Doesn't that work?

J.
-- 
   -----------------
     Jeme A Brelin
    jeme at brelin.net
   -----------------
 [cc] counter-copyright
 http://www.openlaw.org




More information about the PLUG mailing list