Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: how to avoid splitting on an escaped delimiter? Message-ID: <1990Jun5.190758.6804@iwarp.intel.com> Date: 5 Jun 90 19:07:58 GMT References: <1990Jun5.181002.3453@uvaarpa.Virginia.EDU> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 30 In-Reply-To: clindh@abalon.se (Christer Lindh) 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!"=/