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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:46:15+00:00 2026-06-10T03:46:15+00:00

I have a regular expression which has multiple matches. I figured out that $1

  • 0

I have a regular expression which has multiple matches. I figured out that $1 , $2 etc .. can be used to access the matched groups. But how to access the multiple occurences of the same matched group ?

Please take a look at the rubular page below.

http://rubular.com/r/nqHP1qAqRY

So now $1 gives 916 and $2 gives NIL. How can i access the 229885 ? Is there something similar to $1[1] or so ?

  • 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-10T03:46:17+00:00Added an answer on June 10, 2026 at 3:46 am

    To expand on my comment and respond to your question:

    If you want to store the values in an array, modify the block and collect instead of iterate:

    > arr = xml.grep(/<DATA size="(\d+)"/).collect { |d| d.match /\d+/ }
    > arr.each { |a| puts "==> #{a}" }
    ==> 916
    ==> 229885
    

    The |d| is normal Ruby block parameter syntax; each d is the matching string, from which the number is extracted. It’s not the cleanest Ruby, although it’s functional.

    I still recommend using a parser; note that the rexml version would be this (more or less):

    require 'rexml/document'
    include REXML
    doc = Document.new xml
    arr = doc.elements.collect("//DATA") { |d| d.attributes["size"] }
    arr.each { |a| puts "==> #{a}" }
    

    Once your “XML” is converted to actual XML you can get even more useful data:

    doc = Document.new xml
    arr = doc.elements.collect("//file") do |f|
      name = f.elements["FILENAME"].attributes["path"]
      size = f.elements["DATA"].attributes["size"]
      [name, size]
    end
    
    arr.each { |a| puts "#{a[0]}\t#{a[1]}" }
    
    ~/Users/1.txt   916
    ~/Users/2.txt   229885
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following regular expression which is used to give me the tags
I have a regular expression ^[a-zA-Z+#-.0-9]{1,5}$ which validates that the word contains alpha-numeric characters
I have a problem which has an solution that can be solved by iteration,
I have the following regular expression : I figured out most of the part
I need a regular expression that will return an image tag which doesn't have
I have a regular expression which allows the value range '1111' - '999999', excluding
I have a regular expression which uses GroupCollection s in it's capture to capture
I have a regular expression which accept time in a specific format like the
I would like to have a regular expression which clears an HTML field when
I have the following regular expression with which I match a few lines. Here

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.