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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:11:34+00:00 2026-06-17T11:11:34+00:00

I have two types of strings. If a string contains foo first and later

  • 0

I have two types of strings. If a string contains foo first and later bar, it shouldn’t be touched. If it contains only bar, then bar, should be replaced by qux.

  • "sometext foo someothetext bar somethirdtext" shouldn’t be touched
  • "sometext bar someothetext" => "sometext qux someothetext"

It looks like I need to use negative look behind, but I can’t get it working correctly. Currently, I have an expression:

str.gsub! (/(?<!foo)(.*)bar/), '\1qux'

However, it replaces bar to qux in both strings. I have a feeling that .* screws things up. I wasn’t able to find a look behind example where look behind group doesn’t precede matched group immediately.

  • 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-17T11:11:36+00:00Added an answer on June 17, 2026 at 11:11 am

    If you could use variable length lookbehinds, you could just replace matches of /(?<!foo.*)bar/ with 'qux'.

    Without support for variable length lookbehinds, you can really use a lookbehind at all because you can’t know the position to start the lookbehind to find foo. Here is how you can do it with a lookahead:

    str.gsub!  (/\A(((?!foo).)*)bar/m), '\1qux'
    

    Explanation:

    \A           # match at the start of the string
    (            # start capture group 1
      (            # start capture group 2, repeat 0 or more times
        (?!foo)      # fail if 'foo' can match here
        .            # match a single character
      )*           # end capture group 2
    )            # end capture group 1
    bar          # match 'bar'
    

    This performs the negative lookahead (?!foo) at each character until we match bar, so it will not match strings where foo comes before bar.

    The anchor to the beginning of the string is necessary because if you can start in the middle of the string it will just start the match immediately after the f in foo. The multiline option is used so that the . character will match line breaks, not sure if this is necessary for what you are doing.

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

Sidebar

Related Questions

I've got two data types, Foo and Bar , that have a property to
I have two lists: one only contains numbers, while the other contains strings. For
If I have two types of strings as: const char *str1 = This is
So, I have two types at the moment: const unsigned char* unencrypted_data_char; string unencrypted_data;
I have two types of data sets. Both are in same size. One contains
Is it possible to have an array that contains two different types of data?
I have an ObservableCollection<Object> that contains two different types. I want to bind this
let's say I have two xml strings: String logToSearch = <abc><number>123456789012</number></abc> String logToSearch2 =
I have json with field that contains two different types. fields:[{value:ZIELONE OKO},{value:{@nil:true}}] I have
I have a block of text that contains the following types of string that

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.