Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:58:46+00:00 2026-06-17T08:58:46+00:00

I am using Ruby 1.9.3 . I was playing with some patterns and found

  • 0

I am using Ruby 1.9.3. I was playing with some patterns and found something interesting:

Example 1:

irb(main):001:0> /hay/ =~  'haystack'
=> 0
irb(main):003:0> /st/ =~ 'haystack'
=> 3

Example 2:

irb(main):002:0> /hay/.match('haystack')
=> #<MatchData "hay">
irb(main):004:0> /st/.match('haystack')
=> #<MatchData "st">

=~ returns the first location of its first match, whereas match returns the pattern. Other than that, is there any difference between =~ and match()?

Execution time difference (As per @Casper)

irb(main):005:0> quickbm(10000000) { "foobar" =~ /foo/ }
Rehearsal ------------------------------------
   8.530000   0.000000   8.530000 (  8.528367)
--------------------------- total: 8.530000sec

       user     system      total        real
   8.450000   0.000000   8.450000 (  8.451939)
=> nil

irb(main):006:0> quickbm(10000000) { "foobar".match(/foo/) }
Rehearsal ------------------------------------
  15.360000   0.000000  15.360000 ( 15.363360)
-------------------------- total: 15.360000sec

       user     system      total        real
  15.240000   0.010000  15.250000 ( 15.250471)
=> nil
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:58:47+00:00Added an answer on June 17, 2026 at 8:58 am

    First make sure you’re using the correct operator: =~ is correct, ~= is not.

    The operator =~ returns the index of the first match (nil if no match) and stores the MatchData in the global variable $~. Named capture groups are assigned to a hash on $~, and, when the RegExp is a literal on the left side of the operator, are also assigned to local variables with those names.

    >> str = "Here is a string"
    >> re = /(?<vowel>[aeiou])/    # Contains capture group named "vowel"
    >> str =~ re
    => 1
    >> $~
    => #<MatchData "e" vowel:"e">
    >> $~[:vowel]    # Accessible using symbol...
    => "e"
    >> $~["vowel"]    # ...or string
    => "e"
    >> /(?<s_word>\ss\w*)/ =~ str
    => 9
    >> s_word # This was assigned to a local variable
    => " string"
    

    The method match returns the MatchData itself (again, nil if no match). Named capture groups in this case, on either side of the method call, are assigned to a hash on the returned MatchData.

    >> m = str.match re
    => #<MatchData "e" vowel:"e">
    >> m[:vowel]
    => "e"
    

    See http://www.ruby-doc.org/core-1.9.3/Regexp.html (as well as the sections on MatchData and String) for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to ruby and I'm playing around with the IRB. I found that
Using Ruby, I'm trying to parse some documentation in which I need to split
Using ruby-1.9.3... I've read some of the canonical blog posts on the subject of
I'm using Ruby to check the position of videos I'm playing in Quicktime via
Currently I am exploring the world of ruby. Playing around in the irb you
Using Ruby 1.9.2, I have the following Ruby code in IRB: > r1 =
I'm using Ruby's CSV library to parse some CSV. I have a seemingly well-formed
I was playing with some ruby the other day and I wrote the following
I encounter something annoying while playing around with the facebook graph API. I'm using
I have just started playing with Ruby and I'm stuck on something. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.