# String#match vs. Regexp#match - confused

**URL:** <https://www.ruby-forum.com/t/string-match-vs-regexp-match-confused/147733>\
**Category:** Ruby\
**Created:** [September 4, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/string-match-vs-regexp-match-confused/147733 "2008-09-04T19:55:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [September 4, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/string-match-vs-regexp-match-confused/147733/1 "2008-09-04T19:55:53Z")

</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

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [September 4, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/string-match-vs-regexp-match-confused/147733/2 "2008-09-04T20:17:31Z")

</div>

On 9/4/08, Old E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> #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.

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

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

-Adam

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [September 4, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/string-match-vs-regexp-match-confused/147733/3 "2008-09-04T23:03:45Z")

</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.
