Skip to content

MULTILINE confuses re.split #59742

Closed
Closed

Description

@dabrahams
mannequin
BPO 15537
Nosy @ezio-melotti, @dabrahams, @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 2012-08-03.02:41:48.843>
    created_at = <Date 2012-08-02.14:58:56.855>
    labels = ['expert-regex', 'type-bug']
    title = 'MULTILINE confuses re.split'
    updated_at = <Date 2014-10-29.16:13:16.748>
    user = 'https://github.com/dabrahams'

    bugs.python.org fields:

    activity = <Date 2014-10-29.16:13:16.748>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-08-03.02:41:48.843>
    closer = 'ezio.melotti'
    components = ['Regular Expressions']
    creation = <Date 2012-08-02.14:58:56.855>
    creator = 'dabrahams'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 15537
    keywords = []
    message_count = 5.0
    messages = ['167228', '167240', '167243', '167282', '167283']
    nosy_count = 4.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'dabrahams', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '11957'
    type = 'behavior'
    url = 'https://bugs.python.org/issue15537'
    versions = ['Python 2.7']

    Activity

    1. dabrahams commented on Aug 2, 2012

      dabrahamsmannequin
      MannequinAuthor

      compare the output of

      $ python -c "open('/tmp/tst','w').write(100*'x\n');import re;print len(re.split('\n(?=x)', open('/tmp/tst').read()))"
      100

      with

      $ python -c "open('/tmp/tst','w').write(100*'x\n');import re;print len(re.split('\n(?=x)', open('/tmp/tst').read(), re.MULTILINE))"
      9
    2. serhiy-storchaka commented on Aug 2, 2012

      @serhiy-storchaka
      Member
      re.split = split(pattern, string, maxsplit=0, flags=0)
          Split the source string by the occurrences of the pattern,
          returning a list containing the resulting substrings.  If
          capturing parentheses are used in pattern, then the text of all
          groups in the pattern are also returned as part of the resulting
          list.  If maxsplit is nonzero, at most maxsplit splits occur,
          and the remainder of the string is returned as the final element
          of the list.
      
      maxsplit=0 in your fist example and maxsplit=8 (re.MULTILINE is 8) in your second example. This is not a bug, this is a wrong understanding.
    3. mrabarnett commented on Aug 2, 2012

      mrabarnettmannequin
      Mannequin

      There are actually 2 issues here:

      1. The third argument is 'maxsplit', the fourth is 'flags'.

      2. It never splits on a zero-width match. See bpo-3262.

    4. ezio-melotti commented on Aug 3, 2012

      @ezio-melotti
      Member

      See also bpo-11957.

    5. dabrahams commented on Aug 3, 2012

      dabrahamsmannequin
      MannequinAuthor

      Dang! Thanks, and sorry for wasting everyone's time on this.

    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