Skip to content

re.sub does only first 16 replacements if re.S is used #66949

Closed

Description

@mgdelmonte
BPO 22760
Nosy @birkenfeld, @vstinner, @ezio-melotti, @serhiy-storchaka
Superseder
  • bpo-11957: re.sub confusion between count and flags args
  • 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 2014-10-29.15:51:11.671>
    created_at = <Date 2014-10-29.15:35:07.080>
    labels = ['expert-regex', 'type-bug']
    title = 're.sub does only first 16 replacements if re.S is used'
    updated_at = <Date 2014-10-29.16:09:48.499>
    user = 'https://bugs.python.org/mgdelmonte'

    bugs.python.org fields:

    activity = <Date 2014-10-29.16:09:48.499>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-10-29.15:51:11.671>
    closer = 'georg.brandl'
    components = ['Regular Expressions']
    creation = <Date 2014-10-29.15:35:07.080>
    creator = 'mgdelmonte'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 22760
    keywords = []
    message_count = 5.0
    messages = ['230216', '230217', '230218', '230219', '230220']
    nosy_count = 7.0
    nosy_names = ['georg.brandl', 'peter.otten', 'vstinner', 'ezio.melotti', 'mrabarnett', 'serhiy.storchaka', 'mgdelmonte']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '11957'
    type = 'behavior'
    url = 'https://bugs.python.org/issue22760'
    versions = ['Python 2.7']

    Activity

    1. mgdelmonte commented on Oct 29, 2014

      mgdelmontemannequin
      MannequinAuthor

      Easily reproduced:

      re.sub('x', 'a', "x"*20, re.S)

      returns 'aaaaaaaaaaaaaaaaxxxx'

    2. peterotten commented on Oct 29, 2014

      peterottenmannequin
      Mannequin

      This is not a bug; the signature of re.sub() is

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

      and the fourth argument thus the 'count' delimiting the number of substitutions that you accidentally specify as

      >>> import re
      >>> re.S
      16

      I recommend that you use a keyword arg to fix your code:

      >>> re.sub('x', 'a', "x"*20, flags=re.S)
      'aaaaaaaaaaaaaaaaaaaa'
    3. birkenfeld commented on Oct 29, 2014

      @birkenfeld
      Member

      The fourth parameter is not "flags", but "count", the max. number of substitutions. "flags" is the fifth parameter, or give it as a keyword argument.

    4. vstinner commented on Oct 29, 2014

      @vstinner
      Member

      This bug report is common. An enhancement would be to make the count parameter a keyword only parameter. Would it break a lot of code?

    5. ezio-melotti commented on Oct 29, 2014

      @ezio-melotti
      Member
    6. 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