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 8758229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:29:28+00:00 2026-06-13T14:29:28+00:00

Can anybody explain how exactly the back reference works in ruby regular expression? I

  • 0

Can anybody explain how exactly the back reference works in ruby regular expression? I particularly want to know exactly how (..) grouping works. For example:

s = /(..) [cs]\1/.match("The cat sat in the hat")

puts s 

for the code snippet above, the output is: at sat. Why/How is it getting this output ?

  • 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-13T14:29:30+00:00Added an answer on June 13, 2026 at 2:29 pm

    Here is what this regular expression means:

    regex = /(..) [cs]\1/
    #        ├──┘ ├──┘├┘
    #        │    │   └─ A reference to whatever was in the first matching group.
    #        │    └─ A "character class" matching either "c" or "s".
    #        └─ A "matching group" referenced by "\1" containing any two characters.
    

    Note that after matching a regular expression with a matching group, the special variables $1 ($2, etc) will contain what matched.

    /(..) [cs]\1/.match('The cat sat in the hat') # => #<MatchData...>
    $1 # => "at"
    

    Note also that the Regexp#match method returns a MatchData object, which contains the string which caused the entire match (“at sat”, aka $&) and then each matching group (“at”, aka $1):

    /(..) [cs]\1/.match('The cat sat in the hat')
    => #<MatchData "at sat" 1:"at"> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody explain exactly how a web beacon works? I know they're generally used
what exactly are un-managed and managed memory ? can anybody explain me in brief?
Can anybody explain about the SQL Range and List partitions with an example? i
Can anybody explain how this line works? return $y < 0 ? - pip2
Can anybody explain me what exactly Resource manager do. Thanks in advance.
can anybody explain me with simple example to handle Monitor.PulseAll().I have already gone some
Can anybody explain to me why these three codefragments display exactly the same in
Can anybody explain how this function works? public int TestAdd(int a,int b) { if(a
Can anybody explain why we use ? in Collection generics. As for example :
Can anybody explain how the packet interaction with TCP Selective Acknowledgment works? I found

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.