class String
def split_escapable(split_char, escape_char)
arr = []
split(split_char).each do |x|
if(arr[-1] && (/#{escape_char}$/ === arr[-1]))
arr[-1] << x
else
arr << x
end
end
arr
end
end
···
-----Original Message-----
From: Michael Schuerig [mailto:michael@schuerig.de]
Sent: Wednesday, 28 September 2005 9:47 AM
To: ruby-talk ML
Subject: Splitting a string with escapable separator?
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
--
Michael Schuerig All good people read good books
mailto:michael@schuerig.de Now your conscience is clear
http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################