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


Groups > comp.lang.python > #67163

Extracting parts of string between anchor points

From Jignesh Sutar <jsutar@gmail.com>
Date 2014-02-27 20:07 +0000
Subject Extracting parts of string between anchor points
Newsgroups comp.lang.python
Message-ID <mailman.7437.1393531705.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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.

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

for i,s in enumerate(Examples,start=1):
    Out1=s if len(s)>0 else "Empty"
    Out2=s[:s.find(":")+3] + s[s.find("-:-")+5:] if s.find("-:-")>0 else
s.strip() if len(s) else "Empty"
    Out3=s[s.find("-:-")+4:] if s.find("-:-")>0 else
s[s.find(":")+1:].strip() if s.find(":")>0 and len(s)!=s.find(":")+1 else s
if len(s) else "Empty"
    print "Item%(i)s <%(s)s>  Out1 = %(Out1)s" % locals()
    print "Item%(i)s <%(s)s>  Out2 = %(Out2)s" % locals()
    print "Item%(i)s <%(s)s>  Out3 = %(Out3)s" % locals()


Output:

Item1 <Test1A>  Out1 = Test1A
Item1 <Test1A>  Out2 = Test1A
Item1 <Test1A>  Out3 = Test1A
Item2 <Test2A: Test2B>  Out1 = Test2A: Test2B
Item2 <Test2A: Test2B>  Out2 = Test2A: Test2B
Item2 <Test2A: Test2B>  Out3 = Test2B #INCORRECT - Should be "Test2A:
Test2B"
Item3 <Test3A: Test3B -:- Test3C>  Out1 = Test3A: Test3B -:- Test3C
Item3 <Test3A: Test3B -:- Test3C>  Out2 = Test3A: Test3C
Item3 <Test3A: Test3B -:- Test3C>  Out3 = Test3C
Item4 <>  Out1 = Empty
Item4 <>  Out2 = Empty
Item4 <>  Out3 = Empty

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Extracting parts of string between anchor points Jignesh Sutar <jsutar@gmail.com> - 2014-02-27 20:07 +0000
  Re: Extracting parts of string between anchor points Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-28 00:55 +0000
    Re: Extracting parts of string between anchor points Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-28 01:25 +0000

csiph-web