# Weird gsub behavior

**URL:** <https://www.ruby-forum.com/t/weird-gsub-behavior/127005>\
**Category:** Ruby\
**Created:** [January 24, 2008, 3:43am UTC](https://www.ruby-forum.com/t/weird-gsub-behavior/127005 "2008-01-24T03:43:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lzell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@lzell](https://www.ruby-forum.com/u/lzell)\
**Post date:** [January 24, 2008, 3:43am UTC](https://www.ruby-forum.com/t/weird-gsub-behavior/127005/1 "2008-01-24T03:43:44Z")

</div>

Hi,

I came across a problem in a gsub and boiled it down to this difference,  
can anyone explain?

> > b=“trying to match whole string”  
> > =\> “trying to match whole string”

> > b.gsub(/(.+)/,‘MATCH:\1’)  
> > =\> “MATCH:trying to match whole string”

That operated the way I expected, but:

> > b.gsub(/(.\*)/,‘MATCH:\1’)  
> > =\> “MATCH:trying to match whole stringMATCH:”

What is with that second match?

Thanks,  
Lou

---

<div class="post-metadata">

**Author:** ![lzell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@lzell](https://www.ruby-forum.com/u/lzell)\
**Post date:** [January 24, 2008, 4:54am UTC](https://www.ruby-forum.com/t/weird-gsub-behavior/127005/2 "2008-01-24T04:54:21Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![lzell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@lzell](https://www.ruby-forum.com/u/lzell)\
**Post date:** [January 24, 2008, 5:05am UTC](https://www.ruby-forum.com/t/weird-gsub-behavior/127005/3 "2008-01-24T05:05:44Z")

</div>

On Jan 23, 7:43 pm, Lou Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> “MATCH:trying to match whole string”
> 
> That operated the way I expected, but:
> 
> > > b.gsub(/(.\*)/,‘MATCH:\1’)
> 
> =\> “MATCH:trying to match whole stringMATCH:”

.\* means “zero or more of any character (other than a newline), as  
many as you can and still have the rest of the pattern match”.

So first gsub tries to find this, and finds:  
“trying to match whole string”  
so it replaces it:  
“MATCH:trying to match whole string”

Now, the ‘g’ in ‘gsub’ means ‘global’, so it doesn’t stop after just  
one match/replacement. It keeps on going through the string, finding  
more things to replace. At this point, it starts looking for the  
pattern again, this time starting after the ‘g’ in ‘string’. Can it  
find “zero or more of any character (other than a newline)”? Yes, it  
can. It finds zero of them. So then it replaces that (empty string)  
with “MATCH:”, resulting in:  
“MATCH:trying to match whole stringMATCH:”

---

<div class="post-metadata">

**Author:** ![lzell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@lzell](https://www.ruby-forum.com/u/lzell)\
**Post date:** [January 24, 2008, 5:12am UTC](https://www.ruby-forum.com/t/weird-gsub-behavior/127005/4 "2008-01-24T05:12:17Z")

</div>

Excellent, thank you!
