Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Description
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:
If adjacent empty matches are indeed possible as the documentation suggests, could you provide some examples that demonstrate this behavior?
Are there specific scenarios or edge cases where adjacent empty matches can occur?
If possible, could you share a minimal working example that shows how
re.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.