[Tutor] Regular expression on python

Alan Gauld alan.gauld at btinternet.com
Wed Apr 15 09:19:04 CEST 2015


On 15/04/15 02:02, Steven D'Aprano wrote:
>> New one on me. Where does one find out about verbose mode?
>> I don't see it in the re docs?
>>

> or embed the flag in the pattern. The flags that I know of are:
>
> (?x) re.X re.VERBOSE
>
> The flag can appear anywhere in the pattern and applies to the whole
> pattern, but it is good practice to put them at the front, and in the
> future it may be an error to put the flags elsewhere.

I've always applied flags as separate params at the end of the
function call. I've never seen (or noticed?) the embedded form,
and don't see it described in the docs anywhere (although it
probably is). But the re module descriptions of the flags only goive the 
re.X/re.VERBOSE options, no mention of the embedded form.
Maybe you are just supposed to infer the (?x) form from the re.X...

However, that still doesn't explain the difference in your comment
syntax.

The docs say the verbose syntax looks like:

a = re.compile(r"""\d +  # the integral part
                    \.    # the decimal point
                    \d *  # some fractional digits""", re.X)

Whereas your syntax is like:

a = re.compile(r"""(?x)  (?# turn on verbose mode)
                    \d +  (?# the integral part)
                    \.    (?# the decimal point)
                    \d *  (?# some fractional digits)""")

Again, where is that described?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list