# MatchData providing unexpected result

**URL:** <https://www.ruby-forum.com/t/matchdata-providing-unexpected-result/184310>\
**Category:** Rails\
**Created:** [February 28, 2010, 6:17pm UTC](https://www.ruby-forum.com/t/matchdata-providing-unexpected-result/184310 "2010-02-28T18:17:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [February 28, 2010, 6:17pm UTC](https://www.ruby-forum.com/t/matchdata-providing-unexpected-result/184310/1 "2010-02-28T18:17:39Z")

</div>

Hi,

Below are two tests of using MatchData. The first is essentially Hal  
Futon’s taken from The Ruby Way, 2nd. ed. [Thanks, Hal]. In  
particular, m[0] returns the string being searched.

The 2nd is my humble use. For mine, m[0] returns the search pattern,  
it seems,

I can’t anything in the code to account for this difference. I’m  
expecting the first kind of response in a Rails app I’m working on,  
but I’m getting the second kind of response.

Any ideas?

Thanks in Advance,  
Richard

BTW, I running Ruby 1.8.6 over WindowsXP-Pro.SP3

========= Test =========  
puts “\n1.”  
pat = /(.+[aiu])(.+[aiu])(.+[aiu])(.+[aiu])/i  
str = “Fujiyama”  
m = pat.match(str)  
(0…5).to\_a.each { |i| puts “m[%d]: %s” % [i, m[i].to\_s] }

puts “\n2.”  
pat = /noun/i  
str = “The ‘noun’ word is here”  
m = pat.match(str)  
(0…5).to\_a.each { |i| puts “m[%d] %s” % [i, m[i].to\_s] }

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [February 28, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/matchdata-providing-unexpected-result/184310/2 "2010-02-28T18:33:27Z")

</div>

On Sun, Feb 28, 2010 at 12:16 PM, RichardOnRails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> expecting the first kind of response in a Rails app I’m working on,  
> but I’m getting the second kind of response.
> 
> Any ideas?

m[0] doesn’t return the string being searched, it returns everything  
which the entire pattern matched.

In the first case /(.+[aiu])(.+[aiu])(.+[aiu])(.+[aiu])/i entirely  
matches “Fujiyama”

in the second case /noun/i only matches a portion of the string, so  
that’s what is the value of m[0]

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 1, 2010, 12:13pm UTC](https://www.ruby-forum.com/t/matchdata-providing-unexpected-result/184310/3 "2010-03-01T12:13:18Z")

</div>

Hi Rick,

> m[0] doesn’t return the string being searched, it returns everything  
> which the entire pattern matched.

I got it! In Hal’s example, the matched string just happened to be  
the entire string. I didn’t realize that distinction, but I’m on  
board now.

Thanks again for upteenth time for pulling my fat out of the fire.

Best wishes,  
Richard
