# String#match vs. Regexp#match - confused

**URL:** <https://rubytalk.org/t/string-match-vs-regexp-match-confused/48860>\
**Category:** ruby-talk\
**Created:** [4 September 2008 17:50 UTC](https://rubytalk.org/t/string-match-vs-regexp-match-confused/48860 "2008-09-04T17:50:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Old\_Echo](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Old\_Echo](https://rubytalk.org/u/Old_Echo)\
**Post date:** [4 September 2008 17:50 UTC](https://rubytalk.org/t/string-match-vs-regexp-match-confused/48860/1 "2008-09-04T17:50:10Z")

</div>

Hello everyone,  
I'm very puzzled by the following:

a = ".\*me"  
b = /.\*me/  
test = "dont\_ignore.me.fixture"

a.match(test)  
=\> nil  
b.match(test)  
=\> #\<MatchData:0x350fe8\>

Huh? When looking through the ri documentation for the "match" method on  
the String class, it says it take the contents of the String and  
converts it to a regexp before doing a match.

So...how come the two matches do not return the same result? And even  
more importantly, how can I make sure that they do?

Thank you,  
Sebastian

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Adam\_Shelly](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Adam\_Shelly](https://rubytalk.org/u/Adam_Shelly)\
**Post date:** [4 September 2008 18:11 UTC](https://rubytalk.org/t/string-match-vs-regexp-match-confused/48860/2 "2008-09-04T18:11:28Z")

</div>

test.match a  
=\> #\<MatchData:0x283a534\>

The argument is converted to a regexp, not the original String.

-Adam

> **···**
>
> On 9/4/08, Old Echo \<kodama@bluexpanse.net\> wrote:
> 
> > Hello everyone,  
> > I'm very puzzled by the following:
> > 
> > a = ".\*me"  
> > b = /.\*me/  
> > test = "dont\_ignore.me.fixture"
> > 
> > a.match(test)  
> > =\> nil  
> > b.match(test)  
> > =\> #\<MatchData:0x350fe8\>
> > 
> > Huh? When looking through the ri documentation for the "match" method on  
> > the String class, it says it take the contents of the String and  
> > converts it to a regexp before doing a match.

---

<div class="post-metadata">

**Author:** ![Old\_Echo](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Old\_Echo](https://rubytalk.org/u/Old_Echo)\
**Post date:** [4 September 2008 20:58 UTC](https://rubytalk.org/t/string-match-vs-regexp-match-confused/48860/3 "2008-09-04T20:58:01Z")

</div>

Thanks Adam! Yeah...turns out I didn't read the ri docs carefully  
enough.

On the String class, #match takes a regex and compares the original  
string to that. On the Regexp class, #match takes a string and compares  
the pattern to that string.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
