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 ignores flag re.M #86639

Closed
jlaurens mannequin opened this issue Nov 26, 2020 · 2 comments
Closed

re.sub ignores flag re.M #86639

jlaurens mannequin opened this issue Nov 26, 2020 · 2 comments
Labels
3.8 (EOL) end of life topic-IO type-bug An unexpected behavior, bug, or error

Comments

@jlaurens
Copy link
Mannequin

jlaurens mannequin commented Nov 26, 2020

BPO 42473

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-11-26.16:01:39.446>
created_at = <Date 2020-11-26.13:47:10.045>
labels = ['invalid', 'type-bug', '3.8', 'expert-IO']
title = 're.sub ignores flag re.M'
updated_at = <Date 2020-11-26.16:01:39.442>
user = 'https://bugs.python.org/jlaurens'

bugs.python.org fields:

activity = <Date 2020-11-26.16:01:39.442>
actor = 'mrabarnett'
assignee = 'none'
closed = True
closed_date = <Date 2020-11-26.16:01:39.446>
closer = 'mrabarnett'
components = ['IO']
creation = <Date 2020-11-26.13:47:10.045>
creator = 'jlaurens'
dependencies = []
files = []
hgrepos = []
issue_num = 42473
keywords = []
message_count = 2.0
messages = ['381901', '381905']
nosy_count = 2.0
nosy_names = ['mrabarnett', 'jlaurens']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue42473'
versions = ['Python 3.8']

@jlaurens
Copy link
Mannequin Author

jlaurens mannequin commented Nov 26, 2020

Test code:

import re
test='''012345678
         
012345678
'''
pattern = r'^\s+?$'
m = re.search(pattern, test, re.M)
if m:
    print(f'TEST FOUND "{m.span()}"')

    def replace(m):
        print(f'TEST REMOVE {m.span()}')
        return ''

    test = re.sub(pattern, replace, test, re.M)
    m = re.search(pattern, test, re.M)
    if m:
        print(f'TEST STILL THERE "{m.span()}"')

print('COMPILE PATTERN FIRST')
pattern_re = re.compile(pattern, re.M)
m = re.search(pattern_re, test)
if m:
    print(f'TEST FOUND "{m.span()}"')

    def replace(m):
        print(f'TEST REMOVE {m.span()}')
        return ''

    test = re.sub(pattern_re, replace, test)
    m = re.search(pattern_re, test)
    if m:
        print(f'TEST STILL THERE "{m.span()}"')

Actual output:

TEST FOUND "(10, 19)"
TEST STILL THERE "(10, 19)"
COMPILE PATTERN FIRST
TEST FOUND "(10, 19)"
TEST REMOVE (10, 19)

This is an inconsistency between re.search and re.sub. Either this is a bug in the code or in the documentation.

@jlaurens jlaurens mannequin added 3.8 (EOL) end of life topic-IO type-bug An unexpected behavior, bug, or error labels Nov 26, 2020
@mrabarnett
Copy link
Mannequin

mrabarnett mannequin commented Nov 26, 2020

Not a bug.

Argument 4 of re.sub is the count:

    sub(pattern, repl, string, count=0, flags=0)

not the flags.

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

No branches or pull requests

0 participants