Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'yet.': 0.04; 'string.': 0.05; 'string': 0.09; 'if,': 0.09; 'literal': 0.09; 'subject:How': 0.10; 'example?': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'means:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'simplified': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'url:moin': 0.24; 'looks': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'character': 0.29; 'url:wiki': 0.31; 'omitted': 0.31; 'quite': 0.32; 'url:python': 0.33; 'subject:the': 0.34; 'could': 0.34; 'except': 0.35; 'received:84': 0.35; 'there': 0.35; 'hi,': 0.36; 'url:org': 0.36; 'example,': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'quote': 0.39; 'to:addr:python.org': 0.39; 'simple': 0.61; 'first': 0.61; "you've": 0.63; 'more': 0.64; 'between': 0.67; 'link:': 0.72; 'broadcasting': 0.91; 'capture': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=E5NDpMtl c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=LWmzwH2Qa7MA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=xgN9ycDO3QoKiKgszmAA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Thu, 10 Jul 2014 18:01:18 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How to decipher :re.split(r"(\(\([^)]+\)\))" in the example References: <981c1f5f-2c19-4efc-8397-796bde07f39b@googlegroups.com> In-Reply-To: <981c1f5f-2c19-4efc-8397-796bde07f39b@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405011682 news.xs4all.nl 2874 [2001:888:2000:d::a6]:33800 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74318 On 2014-07-10 16:37, 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? > > > Thanks, > > > > > > 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.))" ] > No it doesn't; you've omitted the final string. The regex means: ( Start of capture group. \( Literal "(". \( Literal "(". [^)]+ One or more repeats of any character except a literal ")". \) Literal ")". \) Literal ")". ) End of capture group. .split returns a list of the parts of the string between the matches, and if, as in this example, there are capture groups, then those too: [ 'This is a ', # The part before the first # match. '((test))', # The first match (group 1). ' of the ', # The part between the first # and second matches. '((emergency broadcasting station.))', # The second match. '' # The part after the second # match. ]