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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:27:10+00:00 2026-05-31T16:27:10+00:00

I have a hierarchical datastructure in Django, and want to match the path to

  • 0

I have a hierarchical datastructure in Django, and want to match the path to an object in a Django URL pattern. Here’s my pattern:

url(r'^products/(?P<path>(?:[-\w]+\/?)+)/$',
    CategoriesListView.as_view(model=Product),
    name='product_categories_list'
),

The goal is to match the whole path, but without the trailing slash. The problem is, that on certain input, this regex completely degenerates in performance. The main problem seems to be strings that contain a dot:

In [1]: import re

In [2]: s = re.compile(r'^products/(?P<path>(?:[-\w]+/?)+)/$')

In [3]: s.search('products/111111111111111111111111.c')

This takes around 5 seconds. Making the string longer leads to exponential growth in run time.

How can I rewrite that regex so that it still matches on the same strings, but doesn’t eat my CPU for breakfast?

  • 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-31T16:27:11+00:00Added an answer on May 31, 2026 at 4:27 pm

    You could write:

    r'^products/(?P<path>[-\w/]+)/$'
    

    which matches the same set of strings except that it doesn’t enforce the prohibition on nonterminal //. If that prohibition is important, then you could write:

    r'^products(?!.*//.)/(?P<path>[-\w/]+)/$'
    

    which uses a negative lookahead assertion to enforce that prohibition in a less-expensive way.

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

Sidebar

Related Questions

I'm working on a project where I have some hierarchical data that I want
I have hierarchical data in a nested set model (table:projects): My table (projects): id,
I have hierarchical data stored in the datastore using a model which looks like
I have hierarchical ordered data saved using the modified preorder tree traversal algorithm. Here's
I want to have hierarchical keys like: app:page:user:item = value (where : indicates concatenating
in my project i have hierarchical data and i want to display it in
I have a hierarchical list, like this Menu1,SubMen1.1,SubMen1.2,SubMen1.3 Menu2,SubMen2.1,SubMen2.2,SubMen2.3 Menu3,SubMen3.1,SubMen3.2,SubMen3.3 And i want to
I have a RESTful web service containing hierarchical resources. I want to separate these
I have Hierarchical data. select lpad(' ', 4*(level-1))||NAME from from_sql where where_sql CONNECT BY
I have a hierarchical (tree structure) SQL Server table TEmployee with following columns Id

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.