From: Jason Sweat Date: 2005-09-28T20:02:43+09:00 Subject: Re: Splitting a string with escapable separator? 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"] $ ruby --v ruby 1.9.0 (2005-09-08) [i686-linux] Regards, Jason http://blog.casey-sweat.us/