Skip to content

re.sub ignores flag re.M #86639

Closed
Closed

Description

@jlaurens
mannequin
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']

Activity

  1. jlaurens commented on Nov 26, 2020

    jlaurensmannequin
    MannequinAuthor

    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.

  2. mrabarnett commented on Nov 26, 2020

    mrabarnettmannequin
    Mannequin

    Not a bug.

    Argument 4 of re.sub is the count:

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

    not the flags.

  3. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions