Multisplit: Difference between revisions

1,449 bytes added ,  13 years ago
add Ruby
(→‎{{header|Java}}: This was incorrect, probably not very close to the real solution, and probably confusing to other people)
(add Ruby)
Line 234:
multisplit(S, ["==", "!=", "="]) # output: ['a', [1, 1], '', [0, 3], 'b', [2, 6], '', [1, 7], 'c']
multisplit(S, ["=", "!=", "=="]) # output: ['a', [1, 1], '', [0, 3], '', [0, 4], 'b', [0, 6], '', [1, 7], 'c']</lang>
 
=={{header|Ruby}}==
The simple method, using a regular expression to split the text.
 
<lang ruby>text = 'a!===b=!=c'
separators = ['==', '!=', '=']
 
def multisplit_simple(text, separators)
sep_regex = Regexp.new(separators.collect {|sep| Regexp.escape(sep)}.join('|'))
text.split(sep_regex)
end
 
p multisplit_simple(text, separators)
# => ["a", "", "b", "", "c"]</lang>
 
The version that also returns the information about the separations.
 
<lang ruby>def multisplit(text, separators)
sep_regex = Regexp.new(separators.collect {|sep| Regexp.escape(sep)}.join('|'))
separator_info = []
pieces = []
i = prev = 0
while i = text.index(sep_regex, i)
separator = Regexp.last_match(0)
pieces << text[prev .. i-1]
separator_info << [separator, i]
i = i + separator.length
prev = i
end
pieces << text[prev .. -1]
[pieces, separator_info]
end
 
p multisplit(text, separators)
# => [["a", "", "b", "", "c"], [["!=", 1], ["==", 3], ["=", 6], ["!=", 7]]]</lang>
 
Also demonstrating a method to rejoin the string given the separator information.
 
<lang ruby>def multisplit_rejoin(multisplit_info)
pieces, separator_info = multisplit_info
pairs = pieces.zip(separator_info)
str = pairs.take(pairs.length - 1).inject("") do
|str, (piece, (separator, index))|
str << piece << separator
end
str << pieces.last
end
 
p multisplit_rejoin(multisplit(text, separators)) == text
# => true</lang>
 
=={{header|Tcl}}==
Anonymous user