From: Tanaka Akira Date: 2004-03-24T22:48:29+09:00 Subject: Re: GString and XSS (Re: groovy) In article , Simon Strandgaard writes: > With oniguruma (Rubys new regexp engine) you can use named backreferences. For named capture, "...#{:foo}..." can be used as gsub(/...(?...).../, "...#{:foo}..."). Anyway it avoids backslash escape in the argument. > But that is not what you ask for. Yes. This is another issue. However I considered it too. > Another possibility would be to pass a block to gsub > > str.gsub!(/(\s\w)(\w+)(\w\s)/) do |m| > $3 + $2 + $1 > end > > And then use $1..$9, but it seems more complex. Yes. gsub's block avoids the confusion. However, people tends to gsub without block because it seems simpler. > BTW: Why is 'm' of class String. It would be nice if it were > an instance of MatchData. If it had been a MatchData object, > then the following piece would have worked. > > str.gsub!(/(\s\w)(\w+)(\w\s)/) do |m| > m[3] + m[2] + m[1] > end I think a regexp with named capture should binds local variables. It makes possible to describe your example as follows. str.gsub!(/(?\s\w)(?\w+)(?\w\s)/) { a + b + c } Note that embedded regexp by #{} shouldn't bind local variables. -- Tanaka Akira