From: Bill Kelly Date: 2001-12-17T10:47:29+09:00 Subject: [ruby-talk:28725] Re: Scanning for delimited substrings From: "Martin DeMello" > > I need to scan a string for delimited substrings, and return an array (as in > String#scan) broken into the substrings and the bits between them. For > example, "one <> three <> five six <>" scanned for > ("<<",">>") should return ["one ", "<>", " three ", "<>", > " five six ", "<>"]. The task is made somewhat easier by the fact > that delimiters are strictly not nested ("foo <> quux>>" should > return ["foo ", "<>", " quux"]) How about: scan(/<<.*?>>|(?:.(?!<<))+/) irb(main):001:0> "foo <> quux>>".scan(/<<.*?>>|(?:.(?!<<))+/) ["foo", "<>", " quux>>"] Hmm, note that the above would miss a single character in the case of a starting delimiter with no terminating closing delimiter before the end of the string, as in: irb(main):002:0> "fooa<>|(?:.(?!<<))+/) ["foo", "< "fooa<>|(?:.(?!<<.*?>>))+/) ["fooa<