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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:44:46+00:00 2026-05-28T07:44:46+00:00

I have a regexp that sets $1 : it corresponds to the text between

  • 0

I have a regexp that sets $1 : it corresponds to the text between ( and ) in : the_beginning(.*)the_end.

I want to replace the value corresponding to $1 with somethingelse, not all the regexp.

In real context :

my_string contains :

/* MyKey */ = { [code_missing]; MY_VALUE = "123456789"; [code_missing]; }

I want to replace “123456789” ( with “987654321” for example ).
And this is my regexp :

"/\\* MyKey \\*/ = {[^}]*MY_VALUE = \"(.*)\";"

  • 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-28T07:44:46+00:00Added an answer on May 28, 2026 at 7:44 am

    I’m still not sure exactly what you want, but here’s some code that should help you:

    str = "Hello this is the_beginning that comes before the_end of the string"
    p str.sub /the_beginning(.+?)the_end/, 'new_beginning\1new_end'
    #=> "Hello this is new_beginning that comes before new_end of the string"
    
    p str.sub /(the_beginning).+?(the_end)/, '\1new middle\2'
    #=> "Hello this is the_beginningnew middlethe_end of the string"
    

    Edit:

    theDoc = '/* MyKey */ = { [code_missing]; MY_VALUE = "123456789";'
    regex  = %r{/\* MyKey \*/ = {[^}]*MY_VALUE = "(.*)";}
    p theDoc[ regex, 1 ]   # extract the captured group
    #=> "123456789"
    
    newDoc = theDoc.sub( regex, 'var foo = \1' )
    #=> "var foo = 123456789"  # replace, saving the captured information
    

    Edit #2: Getting access to information before/after a match

    regex = /\d+/
    match = regex.match( theDoc )
    p match.pre_match, match[0], match.post_match
    #=> "/* MyKey */ = { [code_missing]; MY_VALUE = \""
    #=> "123456789"
    #=> "\";"
    
    newDoc = "#{match.pre_match}HELLO#{match.post_match}"
    #=> "/* MyKey */ = { [code_missing]; MY_VALUE = \"HELLO\";"
    

    Note that this requires a regex that does not actually match the pre/post text.

    If you need to specify the limits, and not the contents, you can use zero-width lookbehind/lookahead:

    regex = /(?<=the_beginning).+?(?=the_end)/
    m = regex.match(str)
    "#{m.pre_match}--new middle--#{m.post_match}"
    #=> "Hello this is the_beginning--new middle--the_end of the string"
    

    …but now this is clearly more work than just capturing and using \1 and \2. I’m not sure I fully understand what you are looking for, why you think it would be easier.

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

Sidebar

Related Questions

Suppose I have '/srv/www/site.com/htdocs/system/application/views/' and want to test it against a regexp that matches
I have the following regex that isn't working. I want to match the string
I am fairly new to regexp and have encountered a regexp that delivers an
Brain not working today - can anyone give me a regexp that would turn:
I have the following regexp, that removes BB-tags from string. But I need to
I have two sets of strings ( A and B ), and I want
I have the string 111221 and want to match all sets of consecutive equal
Possible Duplicate: typeof for RegExp I have a routine that is testing to see
I have a string: users/554983490\/Another+Test+/Question????\/+dhjkfsdf/ How would i write a RegExp that would match
I have a regex that is going to end up being a bit long

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.