From: Jimmy Thrasher Date: 2001-10-24T04:07:40+09:00 Subject: [ruby-talk:23072] Re: splitting a string with nested elements At 03:53 AM 10/24/2001 +0900, you wrote: >string = "start {{blah {{ }} outside\n{{ {{ }}\n}} bye" >pre = Regexp.escape("{{") >post = Regexp.escape("}}") ... >which is close to what I want. I really want: >"start " >"blah {{ " >" outside\n" >" {{ }}\n" >" bye" > >Is there a way to do such a thing with a regex, or do I need to resort >to a scanning technique, keeping track of the nested elements? Joe, Technically, what you want can't be done by a regular expression, since it's got nested elements (which are context-free, like Ruby, but not regular). However, it should be pretty simple to do by tokenizing the string and keeping a stack, like so: string = "start {{blah {{ }} outside\n{{ {{ }}\n}} bye" pre = Regexp.escape("{{") post = Regexp.escape("}}") re = /(?:#{pre}|#{post})|(?:[^#{pre}|#{post}])/ string.scan(re) { | match | if match =~ /#{pre}|#{post}/ # do some stack pushing and popping end } Jimmy