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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:59:51+00:00 2026-05-27T04:59:51+00:00

Perhaps a silly question, but though google returned lots of similar cases, I could

  • 0

Perhaps a silly question, but though google returned lots of similar cases, I could not find this exact situation: what regular expression will match all string NOT containing a particular string. For example, I want to match any string that does not contain ‘foo_’.
Now,

 re.match('(?<!foo_).*', 'foo_bar') 

returns a match. While

re.match('(?<!foo_)bar', 'foo_bar')

does not.
I tried the non-greedy version:

 re.match('(?<!foo_).*?', 'foo_bar')

still returns a match.
If I add more characters after the ),

re.search('(?<!foo_)b.*', 'foo_bar')

it returns None, but if the target string has more trailing chars:

re.search('(?<!foo_)b.*', 'foo_barbaric')

it returns a match.
I intentionally kept out the initial .* or .*? in the re. But same thing happens with that.

Any ideas why this strange behaviour? (I need this as a single regular expression – to be entered as a user input).

  • 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-27T04:59:52+00:00Added an answer on May 27, 2026 at 4:59 am

    You’re using lookbehind assertions where you need lookahead assertions:

    re.match(r"(?!.*foo_).*", "foo_bar")
    

    would work (i. e. not match).

    (?!.*foo_) means “Assert that it is impossible to match .*foo_ from the current position in the string. Since you’re using re.match(), that position is automatically defined as the start of the string.

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

Sidebar

Related Questions

This is perhaps a silly question, but I can't seem to find out how
A silly question, perhaps, but at this time of night, StackOverFlow is my only
This is, perhaps, a silly question, but it's something that I've wondered about: is
This is probably a silly question but i haven't been able to find the
I know this is a bit silly question, but I still cannot find an
Perhaps I'm not using the right terminology, but I'm struggling to try to find
Perhaps it is a silly question but I want to be sure about it.
Quick, and perhaps silly question, but here it is nevertheless. Writing to a register
bit of a silly question perhaps. But I want to understand why the syntax
Perhaps I'm being silly asking the question but I need to wrap my head

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.