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


Groups > comp.lang.python > #74316

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; '"this': 0.03; 'resulting': 0.04; 'yet.': 0.04; 'context': 0.07; 'matches': 0.07; '"("': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; '"..."': 0.16; "'a',": 0.16; "'b',": 0.16; 'components.': 0.16; 'delimiters': 0.16; 'example?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'set,': 0.16; 'simplified': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'url:moin': 0.24; 'helpful': 0.24; 'looks': 0.24; 'header:X-Complaints-To:1': 0.27; 'url:wiki': 0.31; 'quite': 0.32; 'url:python': 0.33; 'not.': 0.33; 'subject:the': 0.34; 'could': 0.34; 'but': 0.35; 'hi,': 0.36; 'url:org': 0.36; 'two': 0.37; 'list.': 0.37; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'does': 0.39; 'quote': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'break': 0.61; 'simple': 0.61; 'complete': 0.62; 'more': 0.64; 'link:': 0.72; 'therefore': 0.72; 'special': 0.74; 'group)': 0.84; 'broadcasting': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example
Date Thu, 10 Jul 2014 18:49:28 +0200
Organization None
References <981c1f5f-2c19-4efc-8397-796bde07f39b@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bda429.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
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.11733.1405010988.18130.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405010988 news.xs4all.nl 2951 [2001:888:2000:d::a6]:56078
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74316

Show key headers only | View raw


fl wrote:

> Hi,
> 
> 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.
> It looks very complicated to me. Could you simplified it to a simple
> example?

Just break it into its components.

"(...)" in the context of re.split() keeps the delimiters while just "..." 
does not. Example:

>>> re.split("a+", "abbaaababa")
['', 'bb', 'b', 'b', '']
>>> re.split("(a+)", "abbaaababa")
['', 'a', 'bb', 'aaa', 'b', 'a', 'b', 'a', '']

r"\(" matches the openening parenthesis. The "(" has to be escaped because 
it otherwise has a special meaning (begin group) in a regex.

"[abc]" matches a, b, or c. A leading ^ inverts the set, so "[^abc]" matches 
anything but a, b, or c. Therefore "[^)]" matches anything but the closing 
parenthesis.

The complete regex then is: match two opening parens, then one or more chars 
that are not closing parens, then two closing parens, and make the complete 
group part of the resulting list.

PS: Note that sometimes the re.DEBUG flag may be helpful in understanding 
noisy regexes:

subpattern 1 
  literal 40 
  literal 40 
  max_repeat 1 4294967295 
    not_literal 41 
  literal 41 
  literal 41 
<_sre.SRE_Pattern object at 0x7f5740455c90>

> 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.))" ]

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