From: "Jan E." Date: 2012-03-13T03:40:16+09:00 Subject: Re: Problem replacing $data[abc] with $data['abc'] using gsub The part ".*?" of the regular expression is very inefficient, because it will at first consume every character until the end of the line and then try to find the minimum of characters needed. It's better to make an explicit expression for what you are looking for: /\$data\[([^\]]*)\]/ This reads as "The String '$data[' followed by zero or more characters, which are not closing square brackets, followed by a closing square bracket". Also you don't need the block version of gsub. You can simple use a substitute string and refer to the parenthesized subexpression by \1: string.gsub /\$data\[([^\]]*)\]/, '$data[\'\1\']' Maybe the regular expression should also check if there are already quotes. -- Posted via http://www.ruby-forum.com/.