On 19.10.2006 17:56, Diego Virasoro wrote: > Hello, > given an instance of String, str, how do you select/copy the section of > the string between two given strings START_STRING and END_STRING? > > I used something like: > re = /#{STRING_BEFORE}(regular_expression)#{STRING_AFTER}/ > str =~ re > middle = $1 > where regular_expression is supposed to be a regular expression > selecting any character (digits, space, newline, character, > anything...). > > Is this the right method? And what should I use for regular_expression? > I tried (.|\n)* but I get a "BUS error". Any other way? First, you do not need this alternation if you use /m (multiline mode). This regexp probably does an enourmous amount of backtracking because of this alternation. What's in STRING_BEFORE and STRING_AFTER? If they contain regexp metacharacters you should probably do /#{Regexp.escape STRING_BEFORE}... Depending on the length of your string, you might be better off with something like this: array = str.split(/(#{Regexp.escape STRING_BEFORE}|#{Regexp.escape STRING_AFTER})/) And then extract the middle portion from the resulting array. > Or maybe is there a method which does what I need? > > (Note: I am using Ruby 1.6.4 on Solaris) That version is *ancient*. It might even have some bugs which are fixed in never versions. I strongly recommend to upgrade if possible. Kind regards robert