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? -- Sent from my Nexus 5 with K-9 Mail. Please excuse my brevity.
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.
On 2/12/16 11:15 AM, 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?
Is there a reason to deal with this case at all? Throwing an exception means having to add code to detect the condition and raise the exception. I'd rather see the docs changed to say, "It must be used first in the string, or after one or more whitespace characters." No mention of what happens otherwise is needed. --Ned.
On 12.02.16 23:12, Ned Batchelder wrote:
On 2/12/16 11:15 AM, 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?
Is there a reason to deal with this case at all? Throwing an exception means having to add code to detect the condition and raise the exception.
participants (4)
-
MRAB
-
Ned Batchelder
-
Ryan Gonzalez
-
Serhiy Storchaka