Skip to content

Rule.compile uses non-greedy regex to merge slashes #3121

Closed

Description

@rioj7

The following 2 lines:

rule = re.sub("/{2,}?", "/", self.rule)

path = re.sub("/{2,}?", "/", path)

Uses a non-greedy quantifier. But {2,} suggests it wants to recognize 2 or more repetitions, but {2,}? will only match 2 repetitions.

A small test script:

import re

def _merge_slashes(url:str, re_str:str):
  print(f'    using {repr(re_str)}:', re.sub(re_str, "/", url))

def merge_slashes(url:str):
  print(f'Merge: {url}')
  _merge_slashes(url, "/{2,}?")
  _merge_slashes(url, "/{2,}")

merge_slashes('/path/to/foo')
merge_slashes('//path//to//foo')
merge_slashes('///path///to///foo')
merge_slashes('////path////to////foo')

with output:

Merge: /path/to/foo
    using '/{2,}?': /path/to/foo
    using '/{2,}': /path/to/foo
Merge: //path//to//foo
    using '/{2,}?': /path/to/foo
    using '/{2,}': /path/to/foo
Merge: ///path///to///foo
    using '/{2,}?': //path//to//foo
    using '/{2,}': /path/to/foo
Merge: ////path////to////foo
    using '/{2,}?': //path//to//foo
    using '/{2,}': /path/to/foo

Activity

  1. davidism commented on Mar 6, 2026

    @davidism
    Member

    Makes sense, happy to review a (human written) PR!

  2. added a commit that references this issue on Mar 14, 2026
    34424bb
  3. themavik commented on Mar 19, 2026

    @themavik
  4. added this to the 3.1.7 milestone on Mar 24, 2026
  5. locked as resolved and limited conversation to collaborators on Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions