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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:47:12+00:00 2026-06-13T05:47:12+00:00

I am trying to match part of a file path if it does not

  • 0

I am trying to match part of a file path if it does not include a certain keyword using regular expressions in python. For example, applying the regular expression to “/exclude/this/test/other” should not match, whereas “/this/test/other” should return the file path excluding “other”, i.e. “/this/test”, and where “other” is any directory. So far I am using this

In [153]: re.findall("^(((?!exclude).)*(?=test).*)?", "/exclude/this/test/other")
Out[153]: [('', '')]

re.findall("^(((?!exclude).)*(?=test).*)?", "/this/test/other")
Out[152]: [('/this/test/other', '/')]

but I can’t get it to stop matching after “test”, also there are some empty matches. Any ideas?

  • 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-13T05:47:13+00:00Added an answer on June 13, 2026 at 5:47 am

    You’re getting the extra result because (1) you’re using findall() instead of search(), and (2) you’re using capturing groups instead of non-capturing

    >>> import re
    >>> re.search(r'^(?:(?:(?!exclude).)*(?=test)*)$', "/this/test").group(0)
    '/this/test'
    

    This will work with findall() too, but that doesn’t really make sense when you’re matching the whole string. More importantly, the include part of your regex doesn’t work. Check this:

    >>> re.search(r'^(?:(?:(?!exclude).)*(?=test)*)$', "/this/foo").group(0)
    '/this/foo'
    

    That’s because the * in (?=test)* makes the lookahead optional, which makes it pointless. But getting rid of the * isn’t really a solution, because exclude and test might be part of longer words, like excludexx or yyytest. Here’s a better regex:

    r'^(?=.*/test\b)(?!.*/exclude\b)(?:/\w+)+$'
    

    tested:

    >>> re.search(r'^(?=.*/test\b)(?!.*/exclude\b)(?:/\w+)+$', '/this/test').group()
    '/this/test'
    >>> re.search(r'^(?=.*/test\b)(?!.*/exclude\b)(?:/\w+)+$', '/this/foo').group()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'NoneType' object has no attribute 'group'
    

    EDIT: I see you fixed the “optional lookahead” problem, but now the whole regex is optional!

    EDIT: If you want it to stop matching after /test, try this:

    r'^(?:/(?!test\b|exclude\b)\w+)*/test\b'
    

    (?:/(?!test\b|exclude\b)\w+)* matches zero or more path components, as long as they’re not /test or /exclude.

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

Sidebar

Related Questions

I am trying to delete part of a string that does not match my
I am trying to match a string only if it is not part of
I'm trying to match the whole string and not just part of it. For
I'm following part of this Railscast and trying to load a static file using
I am trying to read from a file and match for a certain combination
I'm trying to match expressions that start with PRE and end with PRE. I'm
I am trying to automate DNS zone creation by using a batch file fired
I'm trying to make sed match the last part of a url and output
I'm trying to implement a hook script in Subversion, using findstr with a regular
I am trying to parse a log file using regex, the problem is 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.