Skip to content

re.sub() replaces only several matches #56287

Closed

Description

@arysin
mannequin
BPO 12078
Nosy @ezio-melotti
Superseder
  • bpo-11957: re.sub confusion between count and flags args
  • Files
  • repl.sh: sed replace which works
  • repl.py: Python replace which does not work
  • test.txt: Text to replace
  • 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 2011-05-14.21:49:39.374>
    created_at = <Date 2011-05-14.15:04:53.100>
    labels = ['expert-regex']
    title = 're.sub() replaces only several matches'
    updated_at = <Date 2011-05-14.22:13:00.721>
    user = 'https://bugs.python.org/arysin'

    bugs.python.org fields:

    activity = <Date 2011-05-14.22:13:00.721>
    actor = 'arysin'
    assignee = 'none'
    closed = True
    closed_date = <Date 2011-05-14.21:49:39.374>
    closer = 'ezio.melotti'
    components = ['Regular Expressions']
    creation = <Date 2011-05-14.15:04:53.100>
    creator = 'arysin'
    dependencies = []
    files = ['21999', '22000', '22001']
    hgrepos = []
    issue_num = 12078
    keywords = []
    message_count = 4.0
    messages = ['135975', '135980', '135992', '135995']
    nosy_count = 3.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'arysin']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '11957'
    type = None
    url = 'https://bugs.python.org/issue12078'
    versions = ['Python 3.1']

    Activity

    1. arysin commented on May 14, 2011

      arysinmannequin
      MannequinAuthor

      When trying to replace strings with parenthesis (in the given example - alternative writing of the words in Ukrainian) the sed replace performs correctly replacing all occurrences, while python3 re.sub() function replaces only first two

    2. mrabarnett commented on May 14, 2011

      mrabarnettmannequin
      Mannequin

      Argument 4 of re.sub is the maximum number of replacements, NOT flags:

      Help on function sub in module re:

      sub(pattern, repl, string, count=0, flags=0)
          Return the string obtained by replacing the leftmost
          non-overlapping occurrences of the pattern in string by the
          replacement repl.  repl can be either a string or a callable;
          if a string, backslash escapes in it are processed.  If it is
          a callable, it's passed the match object and must return
          a replacement string to be used.

      re.I is 2, so you're telling it to perform at most 2 replacements.

    3. ezio-melotti commented on May 14, 2011

      @ezio-melotti
      Member
    4. arysin commented on May 14, 2011

      arysinmannequin
      MannequinAuthor

      Ahh, I guess I should have read the docs. But then looks like I am not the fist or only one so it's not as embarrassing. :)

      I would support the proposal in bpo-11957 to make re flags non-int object

    5. 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

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions