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

  • Home
  • SEARCH
  • 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 370245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:59:57+00:00 2026-05-12T13:59:57+00:00

m = /(.)(.)(\d+)(\d)/.match(THX1138.) puts m[0] c = m.captures #=> HX1138 puts c[0] #=> H

  • 0
m = /(.)(.)(\d+)(\d)/.match("THX1138.")

puts m[0]

c = m.captures #=> HX1138

puts c[0] #=> H
puts  m.begin(0)   #=> 1

puts c[1] #=> X
puts  m.begin(1)   #=> 1

puts c[2] #=> 113
puts  m.begin(2)   #=> 2

I was expecting m.begin(1) to return 2 since X is two elements after the beginning of string.

I am reading the book well grounded rubyist which says

To get the information for capture n,
you provide n as the argument to begin
and/or end.

Similarly I was expecing m.begin(2) to rerturn 3.

  • 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-05-12T13:59:58+00:00Added an answer on May 12, 2026 at 1:59 pm

    Read carefully:

    Returns the offset of the start of the nth element of the match array in the string.

    So the match array is actually [HX1138,H,X,113,8]

    SO

       m.begin(0) => offset of HX1138 => 1 in "THX1138"
       m.begin(1) => offset of H => 1      in "THX1138"
       m.begin(2) => offset of X => 2      in "THX1138"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to match from a a string \begin{?} and \end{?} where ? is
I want to match two different string and output should come in $1 and
I'm trying to match if any string exists after a certain pattern. The pattern
String match = hello; String text = 0123456789hello0123456789; int position = getPosition(match, text); //
I'm trying to match a string against a pattern, but there's one thing I
To match a string with pattern like: -TEXT-someMore-String To get -TEXT- , I came
How can match $string = Foo Bar (Any Group - ANY GROUP Baz); Should
how do we match a string against multiple patters in PCRE ? I need
To match string, which contains some word, I can use pattern /.*word.*/. But how
I am trying to match a @ tags within this string: @sam @gt @channel:sam

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.