[Israel.pm] How to "split" but keep the delimiter

Offer Kaye offer.kaye at gmail.com
Mon Dec 6 03:25:09 PST 2004


On Mon, 6 Dec 2004 12:48:58 +0200, yossi.itzkovich at ecitele.com wrote:
> 
> Hi,
> 
> I know I can do it without "split", but still, by using split, how can I
> still leave the delimiter in the returned list ?
> 
> For example:
> my @out=split (/instance No \d+/ , $text)
> 
> I want to iterate over @out, but still having that \d in place
> 
> Thanks
> 
> Yossi
> 

Save it using "()":
my @out=split (/instance No (\d+)/ , $text)

This is an example from "perldoc -f split":
               If the PATTERN contains parentheses, additional list elements
               are created from each matching substring in the delimiter.

                   split(/([,-])/, "1-10,20", 3);

               produces the list value

                   (1, '-', 10, ',', 20)


Regards,
-- 
Offer Kaye



More information about the Perl mailing list