Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re.sub replaces twice #85727

Closed
spz1st mannequin opened this issue Aug 14, 2020 · 6 comments
Closed

re.sub replaces twice #85727

spz1st mannequin opened this issue Aug 14, 2020 · 6 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@spz1st
Copy link
Mannequin

spz1st mannequin commented Aug 14, 2020

BPO 41555
Nosy @animalize

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-08-17.23:40:00.719>
created_at = <Date 2020-08-14.22:28:33.971>
labels = ['3.8', 'type-bug', '3.7', 'invalid']
title = 're.sub replaces twice'
updated_at = <Date 2020-09-05.12:21:11.896>
user = 'https://bugs.python.org/spz1st'

bugs.python.org fields:

activity = <Date 2020-09-05.12:21:11.896>
actor = 'spz1st'
assignee = 'none'
closed = True
closed_date = <Date 2020-08-17.23:40:00.719>
closer = 'pablogsal'
components = []
creation = <Date 2020-08-14.22:28:33.971>
creator = 'spz1st'
dependencies = []
files = []
hgrepos = []
issue_num = 41555
keywords = []
message_count = 6.0
messages = ['375436', '375444', '375459', '375463', '375475', '376429']
nosy_count = 2.0
nosy_names = ['malin', 'spz1st']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue41555'
versions = ['Python 3.7', 'Python 3.8']

@spz1st
Copy link
Mannequin Author

spz1st mannequin commented Aug 14, 2020

The following command produced "name.tsvtsv" with version 3.7.1 and 3.8.5 instead of the expected "name.tsv" from version 2.7.5, 3.5.6, and 3.6.7. Changing * to + produced expected "name.tsv".

python -c 'import re; v="name.txt";v = re.sub("[^\\.]*$", "tsv", v);print(v)'

@spz1st spz1st mannequin added 3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Aug 14, 2020
@animalize
Copy link
Mannequin

animalize mannequin commented Aug 15, 2020

The re.sub() doc said:
Changed in version 3.7: Empty matches for the pattern are replaced when adjacent to a previous non-empty match.

IMO 3.7+ behavior is more reasonable, and it fixed a bug, see bpo-25054.

@spz1st
Copy link
Mannequin Author

spz1st mannequin commented Aug 15, 2020

Thanks. But if talking about empty matches, there would be endless empty
matches at the end in such cases. So in my opinion, [^\\.]*$ should match
txt plus the empty match because the greedy rule applies here.

On Fri, Aug 14, 2020 at 10:37 PM Ma Lin <report@bugs.python.org> wrote:

Ma Lin <malincns@163.com> added the comment:

The re.sub() doc said:
Changed in version 3.7: Empty matches for the pattern are replaced when
adjacent to a previous non-empty match.

IMO 3.7+ behavior is more reasonable, and it fixed a bug, see bpo-25054.

----------
nosy: +malin


Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41555\>


@animalize
Copy link
Mannequin

animalize mannequin commented Aug 15, 2020

There can be at most one empty match at a position. IIRC, Perl's regex engine has very similar behavior.
If don't want empty match, use + is fine.

@spz1st
Copy link
Mannequin Author

spz1st mannequin commented Aug 15, 2020

Okay. Thanks.

On Sat, Aug 15, 2020 at 9:07 AM Ma Lin <report@bugs.python.org> wrote:

Ma Lin <malincns@163.com> added the comment:

There can be at most one empty match at a position. IIRC, Perl's regex
engine has very similar behavior.
If don't want empty match, use + is fine.

----------


Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41555\>


@spz1st
Copy link
Mannequin Author

spz1st mannequin commented Sep 5, 2020

Thanks.

On Mon, Aug 17, 2020 at 7:40 PM Pablo Galindo Salgado <
report@bugs.python.org> wrote:

Change by Pablo Galindo Salgado <pablogsal@gmail.com>:

----------
resolution: -> not a bug
stage: -> resolved
status: open -> closed


Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41555\>


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant