Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'none,': 0.07; 'none:': 0.07; 'result,': 0.07; 'string': 0.09; 'method,': 0.09; 'none)': 0.09; 'subject:string': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; '":"': 0.16; '%r"': 0.16; '("",': 0.16; '(none,': 0.16; '-tkc': 0.16; 'examples:': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'left,': 0.16; 'left:': 0.16; 'none),': 0.16; 'subject:between': 0.16; 'to:addr:python.list': 0.16; 'to:addr:tim.thechases.com': 0.16; 'to:name:tim chase': 0.16; 'wrote:': 0.18; 'examples': 0.20; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'tim': 0.29; 'chase': 0.31; 'method': 0.36; 'charset:us-ascii': 0.36; 'expected': 0.38; 'skip:p 20': 0.39; 'received:50.22': 0.84; 'result))': 0.84; 'differences': 0.93 Date: Thu, 27 Feb 2014 16:01:51 -0600 From: Tim Chase To: Tim Chase Subject: Re: Extracting parts of string between anchor points In-Reply-To: <20140227154535.6e921956@bigbox.christie.dr> References: <20140227154535.6e921956@bigbox.christie.dr> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com Cc: python-list@python.org 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393538484 news.xs4all.nl 2949 [2001:888:2000:d::a6]:56436 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67170 On 2014-02-27 15:45, Tim Chase wrote: > >>> r = re.compile(r"^([^:]*)(?::((?:(?!-:-).)*)(?:-:-(.*))?)?") If you want to compare both the re method and the string method, here's a test-harness to play with: import re examples = [ ("", (None, None, None)), ("Test1A", ("Test1A", None, None)), ("Test2A: Test2B", ("Test2A", "Test2B", None)), ("Test3A: Test3B -:- Test3C", ("Test3A", "Test3B", "Test3C")), ("Test4A -:- Test4B", None), ("Test5A : Test5B : Test5C -:- Test5D", None), ("Test6A : Test6B -:- Test6C -:- Test6D", None), ] splitter_re = re.compile(r"^([^:]*)(?::((?:(?!-:-).)*)(?:-:-(.*))?)?") def clean(t): return [ s.strip() if s else None for s in t ] def splitter1(s): "using regexp" m = splitter_re.match(s) if m: return tuple(clean(m.groups())) else: return (None, None, None) def splitter2(s): "using string methods" out1 = out2 = out3 = None if ":" in s: if "-:-" in s: left, _, out3 = clean(s.partition("-:-")) if ":" in left: out1, _, out2 = clean(left.partition(":")) else: out1 = left else: out1, _, out2 = clean(s.partition(":")) else: if s: out1 = s return (out1, out2, out3) for method in (splitter1, splitter2): print("") print(method.__doc__) print("=" * len(method.__doc__)) for s, expected in examples: result = method(s) if expected is not None: if result != expected: print("FAIL: %r got %r, not %r" % (s, result, expected)) else: print("PASS: %r got %r" % (s, result)) else: print("UNKN: %r got %r" % (s, result)) Note the differences in Test4. -tkc