Skip to content

re.Scanner groups #84440

Closed
Closed

Description

@dchron
mannequin
BPO 40259
Nosy @ezio-melotti, @tirkarthi
Files
  • re.Scanner.txt
  • 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 = None
    created_at = <Date 2020-04-12.07:52:00.286>
    labels = ['expert-regex', 'type-bug', '3.8']
    title = 're.Scanner groups'
    updated_at = <Date 2020-04-12.14:06:10.839>
    user = 'https://bugs.python.org/dchron'

    bugs.python.org fields:

    activity = <Date 2020-04-12.14:06:10.839>
    actor = 'xtreak'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Regular Expressions']
    creation = <Date 2020-04-12.07:52:00.286>
    creator = 'dchron'
    dependencies = []
    files = ['49049']
    hgrepos = []
    issue_num = 40259
    keywords = []
    message_count = 2.0
    messages = ['366226', '366249']
    nosy_count = 4.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'xtreak', 'dchron']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40259'
    versions = ['Python 3.8']

    Activity

    1. tirkarthi commented on Apr 12, 2020

      @tirkarthi
      Member

      Please add a description of the issue you are facing with a simple script of the behavior.

    2. tirkarthi commented on Apr 12, 2020

      @tirkarthi
      Member

      Copy paste of the contents in the text file

      In the re module there is an experimental feature called Scanner.
      Some unexpected behavior was found while working with it.
      Here is an example:

      >>> re.Scanner([('\w+=(\d+);', lambda s,g: s.match.group(1))]).scan('x=5;')
      (['5;'], '')

      The obvious error is the semicolon returned via capturing group 1.

      Adding a dummy rule at the beginning, seems to solve that issue:

      >>> re.Scanner([('z', None), ('\w+=(\d+);', lambda s,g: s.match.group(1))]).scan('x=5;')
      (['5'], '')

      Adding a capturing group around \w+ also returns the correct answer:

      >>> re.Scanner([('z', None), ('(\w+)=(\d+);', lambda s,g: s.match.group(1))]).scan('x=5;')
      (['x'], '')

      But then, if I ask for the second group, the problem appears again:

      >>> re.Scanner([('z', None), ('(\w+)=(\d+);', lambda s,g: s.match.group(2))]).scan('x=5;')
      (['5;'], '')
    3. transferred this issue fromon Apr 10, 2022
    4. serhiy-storchaka commented on Apr 18, 2022

      @serhiy-storchaka
      Member

      re.Scanner uses groups internally. You should not refer to groups by number unless you know exactly how re.Scanner works internally. Use named groups instead.

      It could be a documentation issue, but re.Scanner is not officially documented, so nothing to do here.

    5. serhiy-storchaka commented on Oct 31, 2025

      @serhiy-storchaka
      Member

      My suggestion to use named groups was incorrect -- no capturing groups are supported in re.Scanner(). Even named group change numbering of groups and mess with capturing groups used by re.Scanner() itself.

    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