Skip to content

re.Scanner doesn't support more than 2 groups on regex #56998

Closed

Description

@angelonuffer
BPO 12789
Nosy @ezio-melotti

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-08-20.18:51:59.901>
created_at = <Date 2011-08-20.03:13:58.785>
labels = ['expert-regex', 'type-bug', 'invalid']
title = "re.Scanner doesn't support more than 2 groups on regex"
updated_at = <Date 2011-08-20.19:00:55.955>
user = 'https://bugs.python.org/angelonuffer'

bugs.python.org fields:

activity = <Date 2011-08-20.19:00:55.955>
actor = 'ezio.melotti'
assignee = 'none'
closed = True
closed_date = <Date 2011-08-20.18:51:59.901>
closer = 'angelonuffer'
components = ['Regular Expressions']
creation = <Date 2011-08-20.03:13:58.785>
creator = 'angelonuffer'
dependencies = []
files = []
hgrepos = []
issue_num = 12789
keywords = []
message_count = 3.0
messages = ['142511', '142543', '142554']
nosy_count = 3.0
nosy_names = ['ezio.melotti', 'mrabarnett', 'angelonuffer']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue12789'
versions = ['Python 2.7']

Activity

  1. angelonuffer commented on Aug 20, 2011

    angelonuffermannequin
    MannequinAuthor

    When I use the scanner object in re module, I can create groups on regex and associate this to a method...

    In [17]: re.Scanner([(r"(\w)(\w)\w", foo)])
    Out[17]: <re.Scanner instance at 0x15c4e60>

    But I tryed 3 groups and it raises "invalid SRE code", but I think my regex is not wrong...

    In [15]: scan = re.Scanner([(r"(\w)(\w)(\w)", foo)])
    ---------------------------------------------------------------------------

    RuntimeError                              Traceback (most recent call last)

    /home/angelo/<ipython console> in <module>()

    /usr/lib/python2.7/re.pyc in __init__(self, lexicon, flags)
    305 s.groups = len(p)+1
    306 p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
    --> 307 self.scanner = sre_compile.compile(p)
    308 def scan(self, string):
    309 result = []

    /usr/lib/python2.7/sre_compile.pyc in compile(p, flags)
    520 return _sre.compile(
    521 pattern, flags | p.pattern.flags, code,
    522 p.pattern.groups-1,
    --> 523 groupindex, indexgroup
    524 )

    RuntimeError: invalid SRE code

  2. mrabarnett commented on Aug 20, 2011

    mrabarnettmannequin
    Mannequin

    Even if this bug is fixed, it still won't work as you expect, and this s why.

    The Scanner function accepts a list of 2-tuples. The first item of the tuple is a regex and the second is a function. For example:

    re.Scanner([(r"\d+", number), (r"\w+", word)])
    

    The Scanner function then builds a regex, using the given regexes as alternatives, each wrapped as a capture group:

    r"(\d+)|(\w+)"
    

    When matching, it sees which group captured and uses that to decide which function it should call, so, for example, if group 1 matched, it calls "number", and if group 2 matched, it calls "word".

    When you introduce capture groups into the regexes, it gets confused. If your regex matches, it'll see that groups 1 and 2 match, so it'll try to call the second function, but there's isn't one...

  3. changed the title [-]re.Scanner don't support more then 2 groups on regex[/-] [+]re.Scanner doesn't support more than 2 groups on regex[/+] on Aug 20, 2011
  4. angelonuffer commented on Aug 20, 2011

    angelonuffermannequin
    MannequinAuthor

    Ah, ok, thanks...
    Then I think my idea is impossible.
    I will use the Scanner in normal way. :)

  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

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions