^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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 <zero>, while the second match is the full string <or more >.
Alex
-----Original Message-----
From: Januski, Ken [mailto:
[email protected]]=20
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')
=3D> "xx"
irb(main):017:0> 'test'.gsub!(/e.*/, 'x')
=3D> "tx"
irb(main):018:0> 'test'.gsub!(/s.*/, 'x')
=3D> "tex"
irb(main):019:0> 'test'.gsub!(/t.*/, 'x')
=3D> "x"
irb(main):020:0> 'test'.gsub!(/st.*/, 'x')
=3D> "tex"
Ken
-----Original Message-----
From: Wybo Dekker [mailto:
[email protected]]=20
Sent: Wednesday, April 02, 2008 5:39 PM
To: ruby-talk ML
Subject: Re: confused by 'test'.gsub(/.*/,'x')
Jens said:
as soon as you anchor the regexp at the beginning of the string it=20
gives the expected result:
=20
irb> 'test'.gsub(/\A.*/) { |m| p m; 'x'}
"test"
=3D>"x"
=20
or just do:
=20
irb> 'test'.sub(/.*/) { |m| p m; 'x'}
"test"
=3D>"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.