# Regex =~ string or string =~ regex?

**URL:** <https://www.ruby-forum.com/t/regex-string-or-string-regex/181039>\
**Category:** Ruby\
**Created:** [January 4, 2010, 10:28am UTC](https://www.ruby-forum.com/t/regex-string-or-string-regex/181039 "2010-01-04T10:28:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 10:28am UTC](https://www.ruby-forum.com/t/regex-string-or-string-regex/181039/1 "2010-01-04T10:28:12Z")

</div>

Sometime I saw you wrote regex =~ string, while sometime you wrote  
string =~ regex.  
What’s their difference and what’s the recommended way? Thanks.

Jenn.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 2:20pm UTC](https://www.ruby-forum.com/t/regex-string-or-string-regex/181039/2 "2010-01-04T14:20:34Z")

</div>

On 01/04/2010 10:27 AM, Ruby N. wrote:

> Sometime I saw you wrote regex =~ string, while sometime you wrote  
> string =~ regex.  
> What’s their difference and what’s the recommended way? Thanks.

The first version invokes method Regexp#=~ and the second version  
invokes String#=~ - which happen to do roughly the same although I  
believe the second one to be a tad slower. I personally prefer the  
first form because of the speed difference and regular expression  
matching is rather an operation of Regexp than of String.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/regex-string-or-string-regex/181039/3 "2010-01-04T19:05:02Z")

</div>

On Mon, Jan 4, 2010 at 6:20 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The first version invokes method Regexp#=~ and the second version invokes  
> String#=~ - which happen to do roughly the same although I believe the  
> second one to be a tad slower. I personally prefer the first form because  
> of the speed difference and regular expression matching is rather an  
> operation of Regexp than of String.

Very technically speaking, calling the Regexp#=~ will be  
microscopically faster because it avoids a tiny bit of C code that  
gets executed when calling the String#=~ version. In practice though,  
the difference is so small as to be invisible against the noise of the  
rest of the system.

String#=~ just has a little sugar built in.

If it’s passed a Regexp, then it just calls the C function underlying  
Regexp#=~.  
If it is passed a String, it complains.  
If it is passed anything else, it tries to call #=~ on what it was  
passed, passing itself as the argument. This lets one build custom  
matching classes that Strings can use with the =~ syntax.

Kirk H.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/regex-string-or-string-regex/181039/4 "2010-01-04T18:43:30Z")

</div>

Robert K. wrote:

> On 01/04/2010 10:27 AM, Ruby N. wrote:
> 
> > Sometime I saw you wrote regex =~ string, while sometime you wrote  
> > string =~ regex.  
> > What’s their difference and what’s the recommended way? Thanks.
> 
> The first version invokes method Regexp#=~ and the second version  
> invokes String#=~ - which happen to do roughly the same although I  
> believe the second one to be a tad slower. I personally prefer the  
> first form because of the speed difference and regular expression  
> matching is rather an operation of Regexp than of String.

Interesting. Whereas I prefer the second because I think  
mystring =~ /foo/  
is syntactically analogous to  
mystring == ‘foo’

> Kind regards
> 
> robert

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
