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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:37:27+00:00 2026-06-03T03:37:27+00:00

I’ve read all related posts and scoured the internet but this is really beating

  • 0

I’ve read all related posts and scoured the internet but this is really beating me.

I have some text containing a date.
I would like to capture the date, but not if it’s preceded by a certain phrase.

A straightforward solution is to add a negative lookbehind to my RegEx.

Here are some examples (using findall).
I only want to capture the date if it isn’t preceded by the phrase “as of”.

19-2-11
something something 15-4-11
such and such as of 29-5-11

Here is my regular expression:

(?<!as of )(\d{1,2}-\d{1,2}-\d{2})

Expected results:

[’19-2-11′]
[’15-4-11′]
[]

Actual results:

[’19-2-11′]
[’15-4-11′]
[‘9-5-11’]

Notice that’s 9 not 29. If I change \d{1,2} to something solid like \d{2} on the first pattern:

bad regex for testing: (?<!as of )(\d{2}-\d{1,2}-\d{2})

Then I get my expected results. Of course this is no good because I’d like to match 2-digit days as well as single-digit days.

Apparently my negative lookbehind is quity greedy — moreso than my date capture, so it’s stealing a digit from it and failing. I’ve tried every means of correcting the greed I can think of, but I just don’t know to fix this.

I’d like my date capture to match with the utmost greed, and then my negative lookbehind be applied. Is this possible? My problem seemed like a good use of negative lookbehinds and not overly complicated. I’m sure I could accomplish it another way if I must but I’d like to learn how to do this.

How do I make Python’s negative lookbehind less greedy?

  • 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-03T03:37:28+00:00Added an answer on June 3, 2026 at 3:37 am

    The reason is not because lookbehind is greedy. This happens because the regex engine tries to match the pattern at every position it can.

    It advances through the phrase such and such as of 29-5-11 successfully matching (?<!as of ) at first, but failing to match \d{1,2}.

    But then the engine finds the itself in the position such and such as of !29-5-11(marked with !). But here it fails to match (?<!as of ).

    And it advances to the next position: such and such as of 2!9-5-11. Where it successfully matches (?<!as of ) and then \d{1,2}.

    How to avoid it?

    The general solution is to formulate the pattern as clear as possible.

    In this very case I would prepend the digit with the necessary space or the beginning of the string.

    (?<!as of)(?:^|\s+)(\d{1,2}-\d{1,2}-\d{2})
    

    The solution of Mark Byers is also very good.

    I think it’s very important to understand the reason why regex engine behaves this way and gives unwanted results.

    By the way the solution I gave above doesn’t work if there are 2 or more spaces.
    It doesn’t work because the fist position matches here such and such as of ! 29-5-11 with the abovementioned pattern.

    What can be done to avoid it?

    Unfortunately lookbehind in Python regex engine doesn’t support quantifiers + or *.

    I think the simplest solution would be to make sure there is not spaces before (?:^|\s+) (meaing that all the spaces are consumed by (?:^|\s+) straight after any nonspace text (and in case the text is as of, terminate advancing and backtrack to the next starting position starting the search all over again at the next position of the searched text).

    re.search(r'(?<!as of)(?<!\s)(?:^|\s+)(\d{1,2}-\d{1,2}-\d{2})','such and such as of  29-5-11').group(1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.