Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Question about adjacent empty matches in regular expressions #122055

Closed
Labels
@krave1986

Description

@krave1986

Documentation

The Pythondocumentation for re.sub() states:

Empty matches for the pattern are replaced only when not adjacent to a previous empty match.

However, after some testing, I have been unable to construct a regular expression pattern that produces adjacent empty matches. This leads to the following questions:

Is it actually possible to create a regular expression pattern that results in adjacent empty matches in Python's re module?
If not, should we consider updating the documentation to avoid potential confusion among developers?

My Investigation

I've tried various patterns that might theoretically produce adjacent empty matches, such as:

importredeffind_all_matches(pattern,string):return [m.start()forminre.finditer(pattern,string)]test_string="abcd"patterns= [r'\b|\b',# Word boundariesr'^|$',# Start or end of stringr'(?=.)|(?<=.)',# Positive lookahead or lookbehindr'.*?',# Non-greedy any character]forpatterninpatterns:matches=find_all_matches(pattern,test_string)print(f"Pattern{pattern}:{matches}")

None of these patterns produce adjacent empty matches. The regex engine seems to always move forward after finding a match, even an empty one.

Request for Clarification

This issue sincerely requests clarification on this matter:

  1. If adjacent empty matches are indeed possible as the documentation suggests, could you provide some examples that demonstrate this behavior?

  2. Are there specific scenarios or edge cases where adjacent empty matches can occur?

  3. If possible, could you share a minimal working example that shows howre.sub() handles adjacent empty matches differently from non-adjacent ones?

These examples would greatly help in understanding the documentation and the behavior of there module in such cases.

Thank you for your time and attention to this matter. Any insights or examples you can provide would be greatly appreciated.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp