regexp weirdness (bug?)

  • Thread starter Sergey Schetinin
  • Start date
S

Sergey Schetinin

Here's the session log:
_re_pair="(?(plus).|-)"
_re1=("(?P<plus>\+)"+_re_pair)
_re2=("((?P<plus>\+))"+_re_pair)
_re3=("(?:(?P<plus>\+))"+_re_pair)
_re4="(%s)"%_re3
import re
print [re.match(_re, "+a") and 'match' for _re in [_re1, _re2,
_re3, _re4]]
['match', None, 'match', None]

this is not the supposed behaivour. all theese patterns should match,
right?
 
G

Guest

* Sergey Schetinin said:
Here's the session log:
_re_pair="(?(plus).|-)"
_re1=("(?P<plus>\+)"+_re_pair)
_re2=("((?P<plus>\+))"+_re_pair)
_re3=("(?:(?P<plus>\+))"+_re_pair)
_re4="(%s)"%_re3
import re
print [re.match(_re, "+a") and 'match' for _re in [_re1, _re2,
_re3, _re4]]
['match', None, 'match', None]

this is not the supposed behaivour. all theese patterns should match,
right?

No, I suppose they shouldn't compile.
_re_pair should be (?P=plus).

nd
 
F

Fredrik Lundh

André Malo said:
No, I suppose they shouldn't compile.
_re_pair should be (?P=plus).

the (?(NAME)RE|RE) form was added in 2.4.

looks like a bug to me; the "plus" group is set to "+" in all four cases, so the
final pattern should match. but I might be missing something...

</F>
 
G

Guest

* Fredrik Lundh said:
the (?(NAME)RE|RE) form was added in 2.4.

looks like a bug to me; the "plus" group is set to "+" in all four cases,
so the
final pattern should match. but I might be missing something...

Uh, yeah, I'm not up to date and still using 2.3 ;)
However, I've investigated a bit in the sre code and found this in
sre_compile.py:

150 elif op is GROUPREF_EXISTS:
151 emit(OPCODES[op])
152 emit((av[0]-1)*2)
153 skipyes = _len(code); emit(0)
154 _compile(code, av[1], flags)

Sergey, can you please change line 152 in your sre_compile.py to

emit(av[0]-1)

and see if it works then? (The matcher also multiplies with 2, so this is
most likely the bug). But it's just theoretically, since I don't have
python 2.4 installed for a test :)

nd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top