Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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; 'skip:[ 20': 0.04; 'none,': 0.07; 'none:': 0.07; 'result,': 0.07; 'suppose': 0.07; 'string': 0.09; 'none)': 0.09; 'omit': 0.09; 'subject:string': 0.09; 'def': 0.12; '":"': 0.16; '%r"': 0.16; '("",': 0.16; "('',": 0.16; '(excluding': 0.16; '(none,': 0.16; '-tkc': 0.16; 'cases:': 0.16; 'examples:': 0.16; 'expression.': 0.16; 'expressions,': 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; 'readable': 0.16; 'regex,': 0.16; 'subject:between': 0.16; 'demonstrate': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'examples': 0.20; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; "i've": 0.25; 'values': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'appreciated.': 0.29; "doesn't": 0.30; "i'm": 0.30; 'gives': 0.31; 'code': 0.31; '(unless': 0.31; 'strip': 0.31; 'regular': 0.32; 'text': 0.33; '(including': 0.33; 'core': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'done,': 0.36; 'edge': 0.36; 'i.e.': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'being': 0.38; 'expected': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'help,': 0.39; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; "you're": 0.61; 'show': 0.63; 'kind': 0.63; 'more': 0.64; 'believe': 0.68; 'expectations': 0.74; 'pass:': 0.84; 'received:50.22': 0.84; 'result))': 0.84 Date: Thu, 27 Feb 2014 15:45:35 -0600 From: Tim Chase To: python-list@python.org Subject: Re: Extracting parts of string between anchor points In-Reply-To: References: 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 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: 110 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393537509 news.xs4all.nl 2829 [2001:888:2000:d::a6]:42609 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67165 On 2014-02-27 20:07, Jignesh Sutar wrote: > I've kind of got this working but my code is very ugly. I'm sure > it's regular expression I need to achieve this more but not very > familiar with use regex, particularly retaining part of the string > that is being searched/matched for. While I suppose this could be done with regular expressions, in this case it seems like a bit of overkill. Just to show that it can be done, I give you this monstrosity: >>> examples = ["Test1A", ... "Test2A: Test2B", ... "Test3A: Test3B -:- Test3C", ... ""] >>> import re >>> r = re.compile(r"^([^:]*)(?::((?:(?!-:-).)*)(?:-:-(.*))?)?") >>> [r.match(s).groups() for s in examples] [('Test1A', None, None), ('Test2A', ' Test2B', None), ('Test3A', ' Test3B ', ' Test3C'), ('', None, None)] You'd still have to strip those values that are strings, but that gets you the core of what you're seeking. You do omit several edge cases: to_test = [ "Test4A -:- Test4D", # no ":" "Test4A : Test4B : Test4C -:- Test4D", # 2x ":" "Test4A : Test4B -:- Test4C -:- Test4D", # 2x "-:-" ] what should Out2 and Out3 be in those particular instances? > Notes and code below to demonstrate what I am trying to achieve. > Any help, much appreciated. > > Examples=["Test1A", > "Test2A: Test2B", > "Test3A: Test3B -:- Test3C", ""] > > # Out1 is just itself unless if it is empty > # Out2 is everything left of ":" (including ":" i.e. part A) and > right of "-:-" (excluding "-:-" i.e. part C) > # If text doesn't contain "-:-" then return text itself as it is > # Out3 is everything right of "-:-" (excluding "-:-" i.e. part C) > # If text doesn't contain "-:-" but does contains ":" then > return part B only > # If it doesn't contain ":" then return itself (unless if it > empty then "None") I believe you want something like examples = [ ("", (None, None, None)), ("Test1A", ("Test1A", None, None)), ("Test2A: Test2B", ("Test2A", "Test2B", None)), ("Test3A: Test3B -:- Test3C", ("Test3A", "Test3B", "Test3C")), # three test-cases with no provided expectations ("Test4A -:- Test4B", None), ("Test5A : Test5B : Test5C -:- Test5D", None), ("Test6A : Test6B -:- Test6C -:- Test6D", None), ] def clean(t): return [ s.strip() if s is not None else s for s in t ] for s, expected in examples: 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 result = (out1, out2, out3) 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)) which gives me PASS: '' got (None, None, None) PASS: 'Test1A' got ('Test1A', None, None) PASS: 'Test2A: Test2B' got ('Test2A', 'Test2B', None) PASS: 'Test3A: Test3B -:- Test3C' got ('Test3A', 'Test3B', 'Test3C') UNKN: 'Test4A -:- Test4B' got ('Test4A', None, 'Test4B') UNKN: 'Test5A : Test5B : Test5C -:- Test5D' got ('Test5A', 'Test5B : Test5C', 'Test5D') UNKN: 'Test6A : Test6B -:- Test6C -:- Test6D' got ('Test6A', 'Test6B', 'Test6C -:- Test6D') I find that a good bit more readable than the atrocity of that regular expression. -tkc