From: "James Edward Gray II" <james / grayproductions.net> > On Nov 4, 2004, at 6:04 PM, Jason Sweat wrote: > > > I expect I am probably missing some facet of Ruby that eaily allows me > > to next regexp inside of the Ruby code in some fasion to achieve the > > result I am looking for, but how to do so eludes me. Can anyone > > provide some insight for me on this situation? > > Ruby's regex engine doesn't yet support this, but will in future > versions. Just for fun, dat = <<-"END_TEXT" /* some stuff */ class foo { public \$var; public function __construct() {} public function bar() { if (false) { } } } // some other stuff END_TEXT repl = [] true while dat.gsub!(/\([^(){}]*\)|\{[^(){}]*\}/) do |str| repl.push(str) "\0#{repl.length - 1}\0" end dat.scan(/class\s+\w+\s+\0\d+\0/) do match = $& true while match.gsub!(/\0(\d+)\0/) { repl[$1.to_i] } puts "Your class matched:", match end The above matches arbitrarily deeply nested ( ) and { } blocks... but all the "classes" it matches have to be at toplevel, can't be inside a { } or ( ) themselves... I don't think it would be hard to modify to handle nested classes, but I haven't thought it through... In case it's not obvious how it works... It replaces, from innermost to outermost, () and {} spans with a token, and saves the original span in an array. When it finds a class match followed by one of these tokens, it expands it back out to its original representation. Regards, Bill