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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:26:36+00:00 2026-05-22T20:26:36+00:00

i would like to match a part of the string using lookarounds, but only

  • 0

i would like to match a part of the string using lookarounds, but only if other words are not contained in the line.

Some bears live and eat in the woods.

in the above line, i’d like to find ” eat in the ” (between “live and” & “woods):

/(?<=\blive and\b).*?(?=\bwoods\b)/

but only if “bears” isn’t apart of the line, either after, before or between the lookarounds.

other examples of lines that should not return a match are:

Some animals live and eat in the
woods, like bears.

Some animals live and eat bears in
the woods.

how can i add this condition to my regular expression?

  • 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-22T20:26:37+00:00Added an answer on May 22, 2026 at 8:26 pm

    This is problematic because most flavors do not support a variable length look-behind, so you can’t check the whole line. A simple approach is to match the whole line instead of using lookarounds:

    ^(?!.*\bbears\b).*?\blive and\b(.*?)\bwoods\b
    

    Here, what previously was the whole match is the first capturing group. Depending on your you use it, it may make replacing this text a little less convenient. Make sure to use the muliline flag (/m), and not the set the single-line flag (dot-all, or /s).

    Working example: http://rubular.com/r/TuADb2vB4w

    Note that the problem becomes very simple if you can solve it in two steps: filter out lines with \bbears\b, and match the wanted string.

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

Sidebar

Related Questions

I would like to remove part of a string. It would have to match
I would like to match a pattern in which two words must be present,
I would like to regex match a sequence of bytes when the string '02
I would like to match URL string anything#1 in django. My regular expression goes
I'd like to test if a regex will match part of a string at
I would like to match the numbers inside an HTML tag such as: Sometext<sometag><htmltag>123123</htmltag></sometag>
I would like to match positive and negative numbers (no decimal or thousand separators)
I have a route like following, ideally I would like it to match: domain.com/layout/1-slug-is-the-name-of-the-page
I would like to know how can I create a regexp to match the
I would like 'about' to route to 'abouts/1' I tried this: match 'about' =>

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.