[Israel.pm] split

Yaakov Belch lists at yaakovnet.com
Tue Feb 24 01:22:36 PST 2004


>
>
>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 
example),
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"'
produces:
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.


Yaakov





More information about the Perl mailing list