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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'yet.': 0.04; 'expressions': 0.07; 'literal': 0.09; 'skip:\\ 10': 0.09; 'subject:How': 0.10; 'python': 0.11; 'bracket': 0.16; 'bracket,': 0.16; 'brackets,': 0.16; 'brackets.': 0.16; 'example?': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'modifier': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simplified': 0.16; 'simpson': 0.16; 'wrote:': 0.18; "python's": 0.19; 'later': 0.20; 'example': 0.22; 'header:User-Agent:1': 0.23; 'tend': 0.24; 'text.': 0.24; 'url:moin': 0.24; 'cheers,': 0.24; 'looks': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'character': 0.29; "doesn't": 0.30; 'characters': 0.30; 'url:wiki': 0.31; 'you?': 0.31; 'bunch': 0.31; 'grouping': 0.31; 'regular': 0.32; 'quite': 0.32; 'url:python': 0.33; 'running': 0.33; 'raw': 0.33; 'subject:the': 0.34; 'could': 0.34; 'except': 0.35; 'but': 0.35; 'add': 0.35; 'licensed': 0.36; 'received:com.au': 0.36; 'charset :us-ascii': 0.36; 'url:org': 0.36; 'two': 0.37; 'received:211': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'explain': 0.39; 'does': 0.39; 'quote': 0.39; 'to:addr:python.org': 0.39; 'simple': 0.61; 'content-disposition:inline': 0.62; 'making': 0.63; 'more': 0.64; 'between': 0.67; 'link:': 0.72; 'special': 0.74; 'heavy': 0.81; 'amateur': 0.84; 'regexp': 0.84; 'rip': 0.84; 'broadcasting': 0.91
Date Fri, 11 Jul 2014 11:29:34 +1000
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
Subject Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Disposition inline
In-Reply-To <981c1f5f-2c19-4efc-8397-796bde07f39b@googlegroups.com>
User-Agent Mutt/1.5.21 (2010-09-15)
References <981c1f5f-2c19-4efc-8397-796bde07f39b@googlegroups.com>
X-Optus-CM-Score 0
X-Optus-CM-Analysis v=2.1 cv=dZS5gxne c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=LWmzwH2Qa7MA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=pGLkceISAAAA:8 a=8AHkEIZyAAAA:8 a=7ghiKHlVAAAA:8 a=YO50ZQfCxtzoqSJeOXIA:9 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A:10 a=s1dkoFP4ry8A:10
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11746.1405042179.18130.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405042179 news.xs4all.nl 2938 [2001:888:2000:d::a6]:32861
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74337

Show key headers only | 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