From: Michael Schuerig Date: 2005-09-28T23:56:44+09:00 Subject: Re: Splitting a string with escapable separator? Jason Sweat wrote: > On 9/27/05, Michael Schuerig wrote: >> >> I'm trying to come up with an *elegant* way to split a string into an >> array at a separator with the additional feature that the separators >> can be escaped. It should work like this >> >> "Hello\, World,Hi".split_escapable(',' '\') >> # => ["Hello, World", "Hi"] >> >> Through a number of permutations with regexps, scan and the rest of >> the family, I was unable to find a solution. I could parse the given >> string myself, going though it character by character, but I'd prefer >> a less pedestrian approach. >> >> Michael > > With the new Regex engine in cvs ruby you can use a negative lookback > assertion in your split: >>> s = "Hello\\, World, Hi" > => "Hello\\, World, Hi" >>> s.split /(? => ["Hello\\, World", " Hi"] That must be the most elegant solution. Unfortunately I can't use cvs ruby and can't wait for it either. Michael -- Michael Schuerig Airtight arguments have mailto:michael@schuerig.de vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions