Skip to content

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

@rioj7

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

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