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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:45:08+00:00 2026-05-18T21:45:08+00:00

A given- I’m using Ruby 1.8.7 and therefor can’t use negative lookbehind. I’m also

  • 0

A given- I’m using Ruby 1.8.7 and therefor can’t use negative lookbehind. I’m also aware of oniguruma but am looking for solutions without it.

If I have:

foo = "string and string [foo and string stuff] string and strings foostring string"
w = "string"

How can I modify this:

foo.gsub(/\b#{w}\b/i) {|s| "[#{w}]"}

So that the ‘string’ enclosed anywhere between [] is not matched, e.g the desired result is:

"[string] and [string] [foo and string stuff] [string] and strings foostring [string]" 

Thanks!

  • 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-18T21:45:09+00:00Added an answer on May 18, 2026 at 9:45 pm

    If you know that the square brackets will always occur in balanced pairs as they do in your example, you can do a negative lookahead for an unbalanced closing bracket after the word. If the opening bracket isn’t after the word, it must be before the word. Example:

    foo.gsub(/\b#{w}\b(?![^\[]*\])/) {|s| "[#{w}]"}
    

    Another option is to match either a pair of brackets and everything inside them, or the target string. If it’s a bracketed sequence you matched, you plug it right back in; otherwise you add brackets to the matched string and plug that in. In this case it’s even simpler: you can just capture everything inside the brackets in one group, or the target string in another group, then use the \+ metasequence to plug in the contents of whichever group matched, with brackets added. Example:

    foo.gsub(/\[([^\]\[]*)\]|(\b#{w}\b)/, '[\+]')
    

    see them in action on ideone

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

Sidebar

Related Questions

Given the above dynamically generated text (meaning that I can't just use an image),
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Given the following string: s = 'abcdefg*' How can I match it or any
Given a storyboard-based application, how can any one view controller invoke the methods of
Given a reference DateTime and given a DateTime to be verified, how can I
Given a file path (e.g. /src/com/mot ), how can I check whether mot exists,
Given the follow: <table id=myTable> <tr> </tr> <tr> </tr> ... </table> I can clear
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given a list of N non-negative integers, propose an algorithm to check if the
Given: http://www.foo.com/bar.html#baz How does one get the baz ? I can't find this as

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.