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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:51:41+00:00 2026-05-26T01:51:41+00:00

I guess this will be a silly mistake but for me, the following returns

  • 0

I guess this will be a silly mistake but for me, the following returns an array containing only “M”. See this:

/(.)+?/.match("Many many characters!").captures
=> ["M"]

Why doesn’t it return an array of every character? I must have missed something blatantly obvious because I can’t see whats wrong with this?

Edit: Just realised, I don’t need the +? but it still doesn’t work without it.

Edit: Apologies! I will clarify: my goal is to allow users to enter a regular expression and styling and an input text file, wherever there is a match, the text will be surrounded with a html element and styling will be applied, I am not just splitting the string into characters, I only used the given regex because it was the simplest although that was stupid on my part. How do I get capture groups from scan() or is that not possible? I see that $1 contains “!” (last match?) and not any others.

Edit: Gosh, it really isn’t my day. As injekt has informed me, the captures are stored in separate arrays. How do I get the offset of these captures from the original string? I would like to be able to get the offset of a captures then surround it with another string. Or is that what gsub is for? (I thought that only replaced the match, not a capture group)

Hopefully final edit: Right, let me just start this again 😛

So, I have a string. The user will use a configuration file to enter a regular expression, then a style associated with each capture group. I need to be able to scan the entire string and get the start and finish or offset and size of each group match.

So if a user had configured ([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}) (email address) then I should be able to get:

[ ["elliotpotts", 0,  11],
  ["sample.",     12, 7],
  ["com",         19, 3] ]

from the string:
“elliotpotts@sample.com”

If that is not clear, there is simply something wrong with me :P. Thanks a lot so far guys, and thank you for being so patient!

  • 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-26T01:51:42+00:00Added an answer on May 26, 2026 at 1:51 am

    Because your capture is only matching one single character. (.)+ is not the same as (.+)

    >> /(.)+?/.match("Many many characters!").captures
    => ["M"]
    >> /(.+)?/.match("Many many characters!").captures
    => ["Many many characters!"]
    >> /(.+?)/.match("Many many characters!").captures
    => ["M"]
    

    If you want to match every character recursively use String#scan or String#split if you don’t care about capture groups

    Using scan:

    "Many many characters!".scan(/./)
    #=> ["M", "a", "n", "y", " ", "m", "a", "n", "y", " ", "c", "h", "a", "r", "a", "c", "t", "e", "r", "s", "!"]
    

    Note that other answer are using (.) whilst that’s fine if you care about the capture group, it’s a little pointless if you don’t, otherwise it’ll return EVERY CHARACTER in it’s own separate Array, like this:

    [["M"], ["a"], ["n"], ["y"], [" "], ["m"], ["a"], ["n"], ["y"], [" "], ["c"], ["h"], ["a"], ["r"], ["a"], ["c"], ["t"], ["e"], ["r"], ["s"], ["!"]]
    

    Otherwise, just use split: "Many many characters!".split(' ')"

    EDIT In reply to your edit:

    reg = /([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/
    str = "elliotpotts@sample.com"
    str.scan(reg).flatten.map { |capture| [capture, str.index(capture), capture.size] }
    #=> [["elliotpotts", 0, 11], ["sample.", 12, 7], ["com", 19, 3]]`
    

    Oh, and you don’t need scan, you’re not really scanning so you dont need to traverse, at least not with the example you provided:

    str.match(reg).captures.map { |capture| [capture, str.index(capture), capture.size] }
    

    Will also work

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

Sidebar

Related Questions

I guess this question will sound familiar, but I am yet another programmer baffled
My guess is that this question will fall under the category of duh, but,
I guess there will be a very simple answer to this. But here goes.
I guess no one will have a definative answer to this but considered predictions
Falks, it is going to be silly mistake but I really cannot guess where
I guess this will sound crazy, but I am reading from a file, and
I'm using Visual Studio, but I guess this will be relevant for other platforms.
I guess my main question is, will this always work as long as I
I guess this is similar to what Facebook does, but...I haven't worked out how
I guess this is kind of an odd question but I have tried setting

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.