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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:36:45+00:00 2026-05-31T15:36:45+00:00

Say I have a string like this some3random5string8 I want to insert spaces after

  • 0

Say I have a string like this

"some3random5string8"

I want to insert spaces after each integer so it looks like this

"some3 random5 string8"

I specifically want to do this using gsub but I can’t figure out how to access the characters that match my regexp.

For example:

temp = "some3random5string8"
temp.gsub(/\d/, ' ')  # instead of replacing with a ' ' I want to replace with
                      # matching number and space

I was hoping there was a way to reference the regexp match. Something like $1 so I could do something like temp.gsub(/\d/, "#{$1 }") (note, this does not work)

Is this possible?

  • 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-31T15:36:46+00:00Added an answer on May 31, 2026 at 3:36 pm

    From the gsub docs:

    If replacement is a String it will be substituted for the matched
    text. It may contain back-references to the pattern’s capture groups
    of the form \d, where d is a group number, or \k, where n is a
    group name. If it is a double-quoted string, both back-references must
    be preceded by an additional backslash.

    This means the following 3 versions will work

    >> "some3random5string8".gsub(/(\d)/, '\1 ')
    => "some3 random5 string8 "
    >> "some3random5string8".gsub(/(\d)/, "\\1 ")
    => "some3 random5 string8 "
    >> "some3random5string8".gsub(/(?<digit>\d)/, '\k<digit> ')
    => "some3 random5 string8 "
    

    Edit: also if you don’t want to add an extra space at the end, use a negative lookahead for the end of line, e.g.:

    >> "some3random5string8".gsub(/(\d(?!$))/, '\1 ')
    => "some3 random5 string8"
    

    A positive lookahead checking for a “word character” would also work of course:

    >> "some3random5string8".gsub(/(\d(?=\w))/, '\1 ')
    => "some3 random5 string8"
    

    Last but not least, the simplest version without a space at the end:

    >> "some3random5string8".gsub(/(\d)(\w)/, '\1 \2')
    => "some3 random5 string8"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let say i have a string like this neverMind<b>What is up</b>neverMind and I want
Say I have a string that looks like this: str = The &yquick &cbrown
Lets say I have a string that represents a date that looks like this:
Say i have a string like this: 23423423 and i want to find all
Let's say you have a string that looks like this: token1 token2 tok3 And
Let's say I have a string like this: ===== and I want to replace
Let say I have a string like this sometext<br>sometext<br>sometext(Rating 50)<br>sometext<br> All I want to
Lets say I have an array like this: string [] Filelist = ... I
Say, I have a code snippet like this: public static void main(String[] args) {
lets say I have a String like this [{ name : Ronald , firstname

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.