[PLUG] Long Perl regex...
Paul Heinlein
heinlein at madboa.com
Thu Sep 18 10:06:02 UTC 2003
On Thu, 18 Sep 2003, Matt Alexander wrote:
> 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/;
Interesting problem. I take it that the text preceding the six digits
is expendable, right?
For starters, ditch the character class and the opening grouping:
s/^.*(\d)\s?(\d)\s?(\d)\s?(\d)\s?(\d)\s?(\d)\s?$/$1$2$3$4$5$6/;
Grouping would allow you to do it in two (shorter) steps:
s/^.*(\d\s?\d\s?\d\s?\d\s?\d\s?\d)\s?$/$1/;
s/\s//g;
This might buy you some maintainability, and it might even be faster,
since you're only producing $1, not $1 .. $7.
If you're up for it, you can make the second half of the regex
executable:
sub nospace { my $x = shift; $x =~ s/\s//g; $x; }
s/^.*((\d)\s?(\d)\s?(\d)\s?(\d)\s?(\d)\s?(\d))\s?$/nospace($1)/e;
But this doesn't buy you anything, and it's likely to be much slower
since you've got a subroutine call in every regex.
--Paul Heinlein <heinlein at madboa.com>
More information about the PLUG
mailing list