[comp.lang.perl] how to avoid splitting on an escaped delimiter?

clindh@abalon.se (Christer Lindh) (06/06/90)

I want to split up a string using several possible delimiters, and
keep the delimiters in the array. The manual says that
   split(/([,-])/, "1-10,20")
gives
   (1, '-', 10, ',', 20)

This works just fine, but how can I AVOID splitting on a delimiter escaped
with backslash?  Ie a split on "1\-10,20" should give ('1\-10', ',', 20).

Has anyone out there got a smart one/many-liner to use in this case?

(I know how to remove the remaining backslashes, for example with
 grep(s/\\(.)/\1/g, @array) )		


-
        clindh@abalon.se              ::     o/      _  _   _ _  _ __  ! 
   Abalon AB, Stockholm, Sweden       ::    /@      !   !   ! !\/! !_! !
                                      ::    <!\     !_  !__ ! !  ! !_! o
     *All disclaimers apply*          ::     !      

merlyn@iwarp.intel.com (Randal Schwartz) (06/06/90)

In article <1990Jun5.181002.3453@uvaarpa.Virginia.EDU>, clindh@abalon (Christer Lindh) writes:
| I want to split up a string using several possible delimiters, and
| keep the delimiters in the array. The manual says that
|    split(/([,-])/, "1-10,20")
| gives
|    (1, '-', 10, ',', 20)
| 
| This works just fine, but how can I AVOID splitting on a delimiter escaped
| with backslash?  Ie a split on "1\-10,20" should give ('1\-10', ',', 20).
| 
| Has anyone out there got a smart one/many-liner to use in this case?
| 
| (I know how to remove the remaining backslashes, for example with
|  grep(s/\\(.)/\1/g, @array) )		

Just cheat a bit...

$_ = "1\\-10,20";
s/\\-/\377/g;
@s = grep(s/\377/-/g || 1, split(/([,-])/));

For \377, just use anything "out of band".  (If you have binary data,
tough.  But, you probably wouldn't be splitting it anyway.)

$,=" "; print +("hacker,","Just","Perl","another")[1,3,2,0];
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/