How to decipher :re.split(r"(\(\([^)]+\)\))" in the example

Roy Smith roy at panix.com
Thu Jul 10 23:33:27 EDT 2014


In article <mailman.11747.1405046292.18130.python-list at python.org>,
 Tim Chase <python.list at tim.thechases.com> wrote:

> On 2014-07-10 22:18, Roy Smith wrote:
> > > Outside this are \( and \): these are literal opening and closing
> > > bracket characters. So:
> > > 
> > >    \(\([^)]+\)\)
> >
> > although, even better would be to use to utterly awesome
> >> re.VERBOSE 
> > flag, and write it as:
> > 
> >      \({2} [^)]+ \){2}
> 
> Or heck, use a multi-line verbose expression and comment it for
> clarity:
> 
>   r = re.compile(r"""
>     (            # begin a capture group
>      \({2}       # two literal "(" characters
>      [^)]+       # one or more non-close-paren characters
>      \){2}       # two literal ")" characters
>     )            # close the capture group
>     """, re.VERBOSE)
> 
> -tkc

Ugh.  That reminds me of the classic commenting anti-pattern:

l = []                  # create an empty list
for i in range(10):     # iterate over the first 10 integers
    l.append(i)         # append each one to the list



More information about the Python-list mailing list