Joao Silva wrote in post #1051235: >> 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\']' > This is what I was looking for. Does this have a name I could Google. It's called backreference. You can also use it in your regular expression to refer to subexpressions previously matched: /(\w)\w*\1/ This matches any string that consists of word characters and begins and ends with the same character. Joao Silva wrote in post #1051235: >> Maybe the regular expression should also check if there are already >> quotes. > Using what you've demonstrated above, would this then be a good way to > exclude strings which contain a leading or trailing quote within the > square brackets? > /\$data\[([^'][^\]]*[^'])\]/ Yes, but you should also exclude double quotes: /\$data\[([^'"][^\]]*[^'"])\]/ Albert's solution is probably even better. As a small modification, I would include possible whitespace: /\$data\[[ \t]*([a-z_][a-z\d_]*)[ \t]*\]/i However, even this expression doesn't cover each and every case. For example, the word in the square brackets could be the identifier of a method or a local variable (then it must not be quoted). But fixing this would probably be too complex. You won't be able to do it with a simple regular expression. -- Posted via http://www.ruby-forum.com/.