From: Warren Brown Date: 2005-09-28T23:58:56+09:00 Subject: Re: Splitting a string with escapable separator? Jason, >> 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. > 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"] With the current Ruby RE engine, you can use zero-width positive lookahead if you don't mind reversing the string before and after the split: irb(main):001:0> s = "Hello\\, World, Hi" => "Hello\\, World, Hi" irb(main):002:0> s.reverse.split(/,(?!\\)/).map {|ss| ss.reverse} => [" Hi", "Hello\\, World"] You might also consider handling escaped escape characters by ignoring pairs of escape characters: irb(main):003:0> s = "Test, Test\\, Test\\\\, Test\\\\\\, Test" => "Test, Test\\, Test\\\\, Test\\\\\\, Test" irb(main):004:0> s.reverse.split(/,(?!(\\\\)*\\([^\\]|$))/).map {|ss| ss.reverse} => [" Test\\\\\\, Test", " Test\\, Test\\\\", "Test"] I hope this helps. - Warren Brown