1.8.7 does not have a negative lookbehind without Oniguruma (which can be compiled).
1.9.3; yay:
> s = "a;b;c\\;d" => "a;b;c\\;d" > s.split /(?<!\\);/ => ["a", "b", "c\\;d"]
1.8.7 with Oniguruma does not offer a trivial split, but you can get offsets and split the substrings this way. I guess the best way to do this is I donβt remember:
> require 'oniguruma' > re = Oniguruma::ORegexp.new "(?<!\\\\);" > s = "hello;there\\;nope;yestho" > re.match_all s => [#<MatchData ";">, #<MatchData ";">] > mds = re.match_all s => [#<MatchData ";">, #<MatchData ";">] > mds.collect {|md| md.offset} => [[5, 6], [17, 18]]
Other options:
- Division into
; and subsequent processing of the search results trailing \\ or - Run a char -by-char loop and maintain some simple state and just split manually.
source share