From: Paul Lutus Date: 2006-12-15T09:05:06+09:00 Subject: Re: Restricted capture in Regexp Benjohn Barnes wrote: /... > I would like gsub's block or second parameter to > provide an array, and for this to replace the captured parts of the > regexp, so: > > "axb".gsub(/(.)x(.)/, ['A', 'B']) > > would return: > > "AxB" > > gsub doesn't behave like this, but I imagine it would be possible to > build a gsub like function that did. :) result = "axb".gsub(/(.)(x)(.)/, "A\\2B" ) # gets what you want. > It would probably need to > inspect the regular expression given to it with a regular expression. Not really. Each of sub(), gsub() and scan() have their niche. It is more a matter of learning how to use them. And, now that I think about it, your example using a provided array of replacement values can be implemented like this: rep = ['A', 'B'] result = "axb".gsub(/(.)(x)(.)/, "#{rep[0]}\\2#{rep[1]}") And, I am sure, in many other ways. -- Paul Lutus http://www.arachnoid.com