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 does only first 16 replacements if re.S is used #66949

Closed
mgdelmonte mannequin opened this issue Oct 29, 2014 · 5 comments
Closed

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

mgdelmonte mannequin opened this issue Oct 29, 2014 · 5 comments
Labels
topic-regex type-bug An unexpected behavior, bug, or error

Comments

@mgdelmonte
Copy link
Mannequin

mgdelmonte mannequin commented Oct 29, 2014

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']

    @mgdelmonte
    Copy link
    Mannequin Author

    mgdelmonte mannequin commented Oct 29, 2014

    Easily reproduced:

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

    returns 'aaaaaaaaaaaaaaaaxxxx'

    @mgdelmonte mgdelmonte mannequin added topic-regex type-bug An unexpected behavior, bug, or error labels Oct 29, 2014
    @peterotten
    Copy link
    Mannequin

    peterotten mannequin commented Oct 29, 2014

    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'

    @birkenfeld
    Copy link
    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.

    @vstinner
    Copy link
    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?

    @ezio-melotti
    Copy link
    Member

    See bpo-11957.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-regex type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants