You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
re.Scanner doesn't support more than 2 groups on regex #56998
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=Noneclosed_at=<Date2011-08-20.18:51:59.901>created_at=<Date2011-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=<Date2011-08-20.19:00:55.955>user='https://bugs.python.org/angelonuffer'
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...
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
angelonuffer commented on Aug 20, 2011
angelonuffermannequin
MannequinAuthor
More actions
Ah, ok, thanks...
Then I think my idea is impossible.
I will use the Scanner in normal way. :)
Activity
angelonuffer commentedon Aug 20, 2011
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)])
---------------------------------------------------------------------------
/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
mrabarnett commentedon Aug 20, 2011
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:
The Scanner function then builds a regex, using the given regexes as alternatives, each wrapped as a capture group:
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...
[-]re.Scanner don't support more then 2 groups on regex[/-][+]re.Scanner doesn't support more than 2 groups on regex[/+]angelonuffer commentedon Aug 20, 2011
Ah, ok, thanks...
Then I think my idea is impossible.
I will use the Scanner in normal way. :)