Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74337

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

Date 2014-07-11 11:29 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example
References <981c1f5f-2c19-4efc-8397-796bde07f39b@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.11746.1405042179.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 10Jul2014 08:37, fl <rxjwg98@gmail.com> wrote:
>This example is from the link:
>
>https://wiki.python.org/moin/RegularExpression
>
>I have thought about it quite a while without a clue yet.
>I notice that it uses
>double quote ", in contrast to ' which I see more often until now.

With raw strings (r', r") this doesn't matter. I tend to use r' myself.

You want raw strings with regular expressions because otherwise their heavy use 
of sloshes "\" overlap with Python's use of sloshes, making everything harder.

>It looks very complicated to me. Could you simplified it to a simple example?
>
>import re
>split_up = re.split(r"(\(\([^)]+\)\))",
>                    "This is a ((test)) of the ((emergency broadcasting station.))")
>
>...which produces:
>
>["This is a ", "((test))", " of the ", "((emergency broadcasting station.))" ]

Rip off the python punctuation and get the regexp itself:

   (\(\([^)]+\)\))

then start from the inside out:

   [^)]  Any character except a closing bracket.
   +     One or more of the preceeding.

Therefore:

   [^)]+ One or more characters which are not closing brackets.
         Also phrased: at least one character which is not a closing bracket.

Outside this are \( and \): these are literal opening and closing bracket 
characters. So:

   \(\([^)]+\)\)
         Two opening brackets, then at least one character which is not a 
         closing bracket, then two closing brackets.

The outermost ( and ) are regexp grouping brackets, not text. On their own you 
don't need them, but they mark out the regexp between them for later reference 
or for use with a repeating modifier like ?, * or +. So in this instance they 
do not add anything special to the regexp.

Given the above inside-to-out explaination, does that explain the re.split 
result for you?

Cheers,
Cameron Simpson <cs@zip.com.au>

I thought the DoD was a bunch of licensed squids. The last thing you
need is a bunch of unregulated, amateur squids running loose.
         - David Wood <davewood@teleport.com>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to decipher :re.split(r"(\(\([^)]+\)\))" in the example fl <rxjwg98@gmail.com> - 2014-07-10 08:37 -0700
  Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Peter Otten <__peter__@web.de> - 2014-07-10 18:49 +0200
  Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example MRAB <python@mrabarnett.plus.com> - 2014-07-10 18:01 +0100
  Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Joel Goldstick <joel.goldstick@gmail.com> - 2014-07-10 13:05 -0400
  Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Albert-Jan Roskam <fomcl@yahoo.com> - 2014-07-10 12:15 -0700
  Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Cameron Simpson <cs@zip.com.au> - 2014-07-11 11:29 +1000
    Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Roy Smith <roy@panix.com> - 2014-07-10 22:18 -0400
      Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Tim Chase <python.list@tim.thechases.com> - 2014-07-10 21:37 -0500
        Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Roy Smith <roy@panix.com> - 2014-07-10 23:33 -0400
          Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Chris Angelico <rosuav@gmail.com> - 2014-07-11 14:31 +1000
          Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example alister <alister.nospam.ware@ntlworld.com> - 2014-07-11 08:00 +0000
          Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Steven D'Aprano <steve@pearwood.info> - 2014-07-11 09:04 +0000
            Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example Albert-Jan Roskam <fomcl@yahoo.com> - 2014-07-11 08:18 -0700

csiph-web