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

LOL, what a creative solution! :slight_smile: I'll have to remember that trick.
"To use lookbehinds in a regexp engine that doesn't have them, reverse the string (and your thinking), then use a lookahead."

···

On Sep 28, 2005, at 8:58 AM, Warren Brown wrote:

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: