
On 2016-02-12 16:15, Ryan Gonzalez wrote:
I was reading re's docs and came across this:
Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.
Is there a particular reason for this being undefined? It seems kind of un-Pythonic to me and more like C/C++. Would it be possible to instead throw an exception, e.g. ValueError?
In Python 3.5 I get this:
re.compile(" (?x)a b", re.DEBUG) LITERAL 32 LITERAL 97 LITERAL 98 LITERAL 97 LITERAL 98 re.compile(' (?x)a b', re.VERBOSE|re.DEBUG)
So, apparently, if the 'flags' argument doesn't specify VERBOSE, it parses the pattern, and if it then finds that it's now marked as VERBOSE because of a "(?m)" in the pattern, it parses it again. Unfortunately, it generates the debug output while it parses, so you get output from both passes.