From: "Bilyk, Alex" Date: 2008-04-03T09:16:31+09:00 Subject: Re: confused by 'test'.gsub(/.*/,'x') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The point is that I don't understand why test.gsub(/.*/,'x') gives me 'xx', since .* means: zero or more of any character, except the newline character, ... - Wybo ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I would venture to say this is exactly what it does. It finds two matches and replaces them both with 'x'. The first match is an empty string , while the second match is the full string . Alex -----Original Message----- From: Januski, Ken [mailto:kjanuski@phillynews.com] Sent: Wednesday, April 02, 2008 3:08 PM To: ruby-talk ML Subject: Re: confused by 'test'.gsub(/.*/,'x') Seems wrong to me as well. If you do a destructive gsub and test for individual letters, e.g. /t.*/,'x', you get 'tex' as you'd expect. Seems wrong to get the double 'x', when you use your example. Of course my background is Perl and I believe that's how it would work there. irb(main):016:0> 'test'.gsub!(/.*/, 'x') => "xx" irb(main):017:0> 'test'.gsub!(/e.*/, 'x') => "tx" irb(main):018:0> 'test'.gsub!(/s.*/, 'x') => "tex" irb(main):019:0> 'test'.gsub!(/t.*/, 'x') => "x" irb(main):020:0> 'test'.gsub!(/st.*/, 'x') => "tex" Ken -----Original Message----- From: Wybo Dekker [mailto:wybo@servalys.nl] Sent: Wednesday, April 02, 2008 5:39 PM To: ruby-talk ML Subject: Re: confused by 'test'.gsub(/.*/,'x') Jens Wille wrote: > as soon as you anchor the regexp at the beginning of the string it > gives the expected result: > > irb> 'test'.gsub(/\A.*/) { |m| p m; 'x'} > "test" > =>"x" > > or just do: > > irb> 'test'.sub(/.*/) { |m| p m; 'x'} > "test" > =>"x" sure, that works, and so does test.gsub(/.+/,'x'). The point is that I don't understand why test.gsub(/.*/,'x') gives me 'xx', since .* means: zero or more of any character, except the newline character, i.e.: all of the string should be replaced with a single x, as far as I can see. -- Wybo