# Python thing that doesn't work in Sage, works in pure Python

 2 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? asked Jun 14 '12 kcrisman 6784 ● 14 ● 67 ● 152 This one has gotten me, too. It's very annoying.John Palmieri (Jun 19 '12)See here.bk322 (Jun 19 '12)@bk322 - right, if I'd gotten that error I would have thought of this, but it was the IndexError that made it so weird. Because of course this error naturally appears anyway when you've tried to access a nonexistent group (like matches.group(3) would be above, I guess)kcrisman (Jun 19 '12)

 3 Never mind. sage: matches.group(int(1)) 'bar'  Leaving this up for those who might stumble. I'll try to think of a better title for the question so people find it. This seems bad, though. I suppose there isn't any way to notice when we are using "pure Python" commands and for the preparser to at least try not doing the whole Integer thing if it gets an Error of some kind? Maybe that would lead to more chaos than we'd like. posted Jun 14 '12 kcrisman 6784 ● 14 ● 67 ● 152
 3 For others that stumble across this question, you can turn the Sage preparser off by calling preparser(False). Then the code above works just fine at the Sage command line: sage: preparser(False) 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) 'foobarsws' sage: matches.group(1) 'bar'  posted Jun 18 '12 benjaminfjones 2500 ● 3 ● 34 ● 66 http://bfj7.com/

[hide preview]