[Israel.pm] split

Yossi.Itzkovich at ecitele.com Yossi.Itzkovich at ecitele.com
Tue Feb 24 01:29:00 PST 2004

Thanks for the detailed answer.

                      Yaakov Belch                                                                                                 
                      <lists at yaakovnet         To:      Perl in Israel <perl at perl.org.il>                                          
                      .com>                    cc:                                                                                 
                      Sent by:                 Subject: Re: [Israel.pm] split                                                      
                      perl-bounces at per                                                                                             
                      02/24/2004 11:22                                                                                             
                      Please respond                                                                                               
                      to Perl in                                                                                                   

>Is there a way to get split()  to return the delimiter too ?
>For the string  "deabcamn" , and using the delimiter "a" , I want to get :
>qw(de abc amn).
Depending on what you want to get, there are two ways:

If you want the delimiter to be attached to the strings (as in your
you need a "zero-width positive look-ahead assertion" as explained in
   perldoc perlre
(search for the string "ahead").  For your example, I just typed into
the shell:
perl -we '@res=split /(?=a)/, "deabcamn"; print "@res\n"'
The result is:
de abc amn

Another option is explained in
  perldoc -f split
(search for the string "parentheses"): You get the delimter as separate
elements of the output list.  For example,
perl -we '@res= split /(a)/, "deabcamn"; print "@res\n"'
de a bc a mn

Of course, in both cases the delimiter can include variable elements
like . or \S,
so the results can contain information that you want to process.


Perl mailing list
Perl at perl.org.il


More information about the Perl mailing list