in reply to Re: Split a string into items of constant length
in thread Split a string into items of constant length
Why using an extended regex to use split where a simple match would do?my $string = "a(b)cd(e)f"; my @parts = split /(?=.{5}$)/, $string';
local $_ = "a(b)cd(e)f"; my @parts = split /.{5}/g;
(Yes: of course this does not take care of the case when the given string has a length that is not a multiple of 5. But then I'm using unpack, as well as Perl Mouse does!)local $_ = "a(b)cd(e)f"; my @parts = /.{5}/g;
Update: fixed a split that I had inadvertently left in -- see stroken out code above. Thanks to sauog's comment.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Split a string into items of constant length
by truedfx (Monk) on Oct 04, 2005 at 13:40 UTC | |
Re^3: Split a string into items of constant length
by sauoq (Abbot) on Oct 04, 2005 at 10:47 UTC |