Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Python regex trouble in Sage, not Python

In sage -python:

>>> import re
>>> ST = "foobarsws"
>>> matches = re.match("(?:foo)?(bar)?(sws)?",ST)
>>> matches.groups()
('bar', 'sws')
>>> matches.group(0)
'foobarsws'
>>> matches.group(1)
'bar'
>>> matches.group(2)
'sws'

ST = "file://localhost/foo.sws\nfile:///bar.sws" matches = re.match("file://(?:localhost)?(/.+)",ST) matches.groups() ('/foo.sws',) matches.group() 'file://localhost/foo.sws' matches.group(0) 'file://localhost/foo.sws' matches.group(1) '/foo.sws'

In Sage:

sage: import re
sage: ST = "foobarsws"
sage: matches = re.match("(?:foo)?(bar)?(sws)?",ST)
sage: matches.groups()
('bar', 'sws')
sage: matches.group()
'foobarsws'
sage: matches.group(0)
IndexError: no such group
sage: matches.group(1)
IndexError: no such group
sage: matches.group(2)
IndexError: no such group

Any ideas?

Python regex trouble in Sage, not Python

In sage -python:

>>> import re
>>> ST = "foobarsws"
>>> matches = re.match("(?:foo)?(bar)?(sws)?",ST)
>>> matches.groups()
('bar', 'sws')
>>> matches.group(0)
'foobarsws'
>>> matches.group(1)
'bar'
>>> matches.group(2)
'sws'

ST = "file://localhost/foo.sws\nfile:///bar.sws" matches = re.match("file://(?:localhost)?(/.+)",ST) matches.groups() ('/foo.sws',) matches.group() 'file://localhost/foo.sws' matches.group(0) 'file://localhost/foo.sws' matches.group(1) '/foo.sws'

In Sage:

sage: import re
sage: ST = "foobarsws"
sage: matches = re.match("(?:foo)?(bar)?(sws)?",ST)
sage: matches.groups()
('bar', 'sws')
sage: matches.group()
'foobarsws'
sage: matches.group(0)
IndexError: no such group
sage: matches.group(1)
IndexError: no such group
sage: matches.group(2)
IndexError: no such group

Any ideas?

Python regex trouble thing that doesn't work in Sage, not works in pure Python

In sage -python:

>>> import re
>>> ST = "foobarsws"
>>> matches = re.match("(?:foo)?(bar)?(sws)?",ST)
>>> matches.groups()
('bar', 'sws')
>>> matches.group(0)
'foobarsws'
>>> matches.group(1)
'bar'
>>> matches.group(2)
'sws'

In Sage:

sage: import re
sage: ST = "foobarsws"
sage: matches = re.match("(?:foo)?(bar)?(sws)?",ST)
sage: matches.groups()
('bar', 'sws')
sage: matches.group()
'foobarsws'
sage: matches.group(0)
IndexError: no such group
sage: matches.group(1)
IndexError: no such group
sage: matches.group(2)
IndexError: no such group

Any ideas?