From: "Brian Schröder" Date: 2004-10-05T18:09:06+09:00 Subject: Re: Regexp match question on interpolated strings... Richard Kilmer wrote: > If I had the source for a string: > > "name = #{person.first_name+" "+person.last_name} ... Ok?" > > And assuming I could find the first and last double quotes, how would I > parse out the #{ ... } with a regular expression since anything can fall > between the #{ ... } braces in a string? > > Thanks in advance. > > -rich > > > Regular expressions are not able to "count" more than a finite number of states, and the number of states is fixed at compile time. That is because regular expressions map to finite automata. So it is impossible to match opening and closing braces in an unknown expression. For this to work always you need a model that can enter unbounded many states. But beware, your computer is also only a finite state machine with a lot of states. The number of its states is bounded by the size of ram (and harddisk). If you are shure that there will be no closing braces inside of the braces you could match /\#\{(.*?)\}/ =~ string or including at most one pair of inside braces /\#\{([^\{}]*(\{.*?\}|).*?)\}/ =~ string As you see it begins to get ugly now. Regards, Brian -- Brian Schr�der http://ruby.brian-schroeder.de/