From: Bill Kelly Date: 2001-12-17T11:22:23+09:00 Subject: [ruby-talk:28727] Re: Scanning for delimited substrings From: "Martin DeMello" [...] > Also, I left out nested delimiters because I don't need them, and I couldn't > think of a clean way to add them in without slowing things down, but if > there's a more general method that trivially handles both cases I'd like to > see it. Here's a thought: (not thoroughly tested (!)) def nested_delim_scan(txt) tokens = [] nestLvl = 0 curTok = "" txt.scan(/<<|>>|(?:(?!<<|>>).)+/) { |s| curTok << s nestLvl += 1 if s == '<<' nestLvl -= 1 if s == '>>' if nestLvl < 1 tokens.push curTok nestLvl = 0 curTok = "" end } tokens.push curTok unless curTok == "" tokens end irb(main):001:0> nested_delim_scan("foo <> quux>>") ["foo ", "<> quux>>"] irb(main):002:0> nested_delim_scan("foo <> quux") ["foo ", "<> quux"] irb(main):003:0> nested_delim_scan("foo <> quux>> fleen >>") ["foo ", "<> quux>>", " fleen ", ">>"] Regards, Bill