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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:46:38+00:00 2026-06-11T10:46:38+00:00

I have a string str = ‘${1:textbox}’,[${2:x},${3:y},${4:w},${5:h}] and I would like to replace all

  • 0

I have a string

str = "'${1:textbox}',[${2:x},${3:y},${4:w},${5:h}]"

and I would like to replace all , between [ and ] with a single space.

I have attempted to use something like

str.gsub!(/(?<=\[)\,*?(?=\])/," ")

without success. However, if I replace \, in my expression with ., I get the expected output:

str.gsub!(/(?<=\[).*?(?=\])/," ")

== "'${1:textbox}',[  ]"

Could someone please explain the proper regex technique to use in this situation, and perhaps also explain why the examples I have posted above have failed and succeeded?

I am using ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]

  • 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-11T10:46:40+00:00Added an answer on June 11, 2026 at 10:46 am

    It may be possible to do this with a single regex, but even if it is, I can guarantee it’ll be ugly beyond description. It’s a lot simpler to use “nested” substitution – use one gsub to find bracketed substrings, and then use another to swap out the commas:

    str.gsub(/\[.*?\]/) do |substr|
        substr.gsub(',', ' ')
    end
    

    I’m afraid I can’t explain why your attempts have failed – neither of them would run for me (ruby 1.8.7 / irb 0.9.5). IRB gave errors that vaguely said “Bad regexp syntax.” And I can’t quite grok how they’re supposed to work (edit: mu is too short has an awesome breakdown in his answer – check that out). Hope this is helpful anyway!

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

Sidebar

Related Questions

Supposed I have the following string: string str = <tag>text</tag>; And I would like
I have String: String str = KITTEN; And I would like to make a
I have a string like: string str = https://abce/MyTest; I want to check if
I have a string like the following: String str = 4*5; Now I have
I have a string like this String str = la$le\\$li$lo . I want to
I have a string containing lots of text with white-spaces like: String str =
I have a string str = txtName.Text (txtName is name of textbox control) How
I have a string like below string str = ABE,1A2B3CD,1B2A3C,1A2BC3E; How can i split
I have a string something like, string str = (50%silicon +20%!(20%Gold + 80%Silver)| +
I have following string String str = replace :) :) with some other string;

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.