[issue41080] re.sub treats * incorrectly?

2020-06-22 Thread Ryan Westlund


New submission from Ryan Westlund :

```
>>> re.sub('a*', '-', 'a')
'--'
>>> re.sub('a*', '-', 'aa')
'--'
>>> re.sub('a*', '-', 'aaa')
'--'
```

Shouldn't it be returning one dash, not two, since the greedy quantifier will 
match all the a's? I understand why substituting on 'b' returns '-a-', but 
shouldn't this constitute only one match? In Python 2.7, it behaves as I expect:

```
>>> re.sub('a*', '-', 'a')
'-'
>>> re.sub('a*', '-', 'aa')
'-'
>>> re.sub('a*', '-', 'aaa')
'-'
```

The original case that led me to this was trying to normalize a path to end in 
one slash. I used `re.sub('/*$', '/', path)`, but a nonzero number of slashes 
came out as two.

--
components: Regular Expressions
messages: 372104
nosy: Yujiri, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: re.sub treats * incorrectly?
type: behavior
versions: Python 3.10, Python 3.7, Python 3.8

___
Python tracker 
<https://bugs.python.org/issue41080>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41080] re.sub treats * incorrectly?

2020-06-22 Thread Ryan Westlund


Ryan Westlund  added the comment:

Sorry, I forgot the pydoc docs don't have as much information as the online
docs.

On Mon, Jun 22, 2020 at 1:54 PM Ezio Melotti  wrote:

>
> Ezio Melotti  added the comment:
>
> This behavior was changed in 3.7: "Empty matches for the pattern are
> replaced only when not adjacent to a previous empty match, so sub('x*',
> '-', 'abxd') returns '-a-b--d-'." [0]
>
> See also bpo-32308 and bpo-25054.
>
>
> [0]: https://docs.python.org/3/library/re.html#re.sub
>
> --
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
> superseder:  -> Replace empty matches adjacent to a previous non-empty
> match in re.sub()
>
> ___
> Python tracker 
> <https://bugs.python.org/issue41080>
> ___
>

--

___
Python tracker 
<https://bugs.python.org/issue41080>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com