开发者

When using the $1-$9 regular expression form, why does calling gsub(...) on one nil the others?

开发者 https://www.devze.com 2023-01-13 16:50 出处:网络
First, a working example: string = \"foo-bar-25-baz\" if string =~ /(.+)-(10|25)(?:-(baz))?/ puts $1 puts $2

First, a working example:

string = "foo-bar-25-baz"
if string =~ /(.+)-(10|25)(?:-(baz))?/
  puts $1 
  puts $2
  puts $3
end

This produces 'foo-bar', '25' and 'baz' on three lines, as expected. But if we do this:

string = "foo-bar-25-baz"
if string =~ /(.+)-(10|25)(?:-(baz))?/
  puts $1.gsub('-', ' ') # Her开发者_如何学Goe be the problem
  puts $2 # nil
  puts $3 # nil
end

the values of $2 and $3 are now nil. I have to puts $2 and puts $3 and then $1.gsub(...), and it will work. As far as I can tell this only applies to gsub and gsub!

This causes the same problem:

string = "foo-bar-25-baz"
if string =~ /(.+)-(10|25)(?:-(baz))?/
  puts $3.gsub('hard', 'h')
  puts $1 # nil
  puts $2 # nil
end

I spent about 15 minutes debugging this and I'm wondering why.


gsub is most likely re-assigning those variables (as would pretty much any other function that uses the regexp engine). If you need to call gsub before using all your original match results, store them to a local variable first with something like match_results = [$1, $2, $3].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号