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.Scanner groups #84440

Closed
dchron mannequin opened this issue Apr 12, 2020 · 3 comments
Closed

re.Scanner groups #84440

dchron mannequin opened this issue Apr 12, 2020 · 3 comments
Labels
3.8 only security fixes topic-regex type-bug An unexpected behavior, bug, or error

Comments

@dchron
Copy link
Mannequin

dchron mannequin commented Apr 12, 2020

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

    @dchron dchron mannequin added 3.8 only security fixes topic-regex type-bug An unexpected behavior, bug, or error labels Apr 12, 2020
    @tirkarthi
    Copy link
    Member

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

    @tirkarthi
    Copy link
    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;'], '')

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @serhiy-storchaka
    Copy link
    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.

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

    No branches or pull requests

    2 participants