Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67164
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| 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; "'',": 0.07; 'variables': 0.07; 'string': 0.09; 'arrays': 0.09; 'parsing': 0.09; 'subject:string': 0.09; '":"': 0.16; "':'": 0.16; '(completely': 0.16; '(excluding': 0.16; 'arithmetic.': 0.16; 'charles': 0.16; 'cryptic': 0.16; 'empty,': 0.16; 'foo.': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'readable': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'regex,': 0.16; 'simpson': 0.16; 'splitting': 0.16; 'subject:between': 0.16; 'uppercase': 0.16; 'demonstrate': 0.16; 'index': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; 'split': 0.19; 'written': 0.21; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'tend': 0.24; 'text.': 0.24; 'cheers,': 0.24; "i've": 0.25; 'second': 0.26; 'header:In-Reply- To:1': 0.27; 'appear': 0.29; 'appreciated.': 0.29; 'leave': 0.29; 'words': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; '(unless': 0.31; 'minor': 0.31; 'ordinary': 0.31; 'class': 0.32; 'regular': 0.32; 'quite': 0.32; 'text': 0.33; '(including': 0.33; 'everyone': 0.33; "can't": 0.35; 'etc': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'i.e.': 0.36; 'received:com.au': 0.36; 'useful': 0.36; 'charset:us-ascii': 0.36; 'easily': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'received:211': 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; 'easy': 0.60; 'expression': 0.60; 'lower': 0.61; "you're": 0.61; 'content- disposition:inline': 0.62; 'kind': 0.63; 'more': 0.64; 'funny': 0.74; 'habit': 0.91 |
| Date | Fri, 28 Feb 2014 08:40:12 +1100 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | python-list@python.org |
| Subject | Re: Extracting parts of string between anchor points |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Disposition | inline |
| In-Reply-To | <CACvW2fx8y27p8+gs3BVrRe2=1FL4+wH30c+MA1JaYsyLNFyfMg@mail.gmail.com> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| References | <CACvW2fx8y27p8+gs3BVrRe2=1FL4+wH30c+MA1JaYsyLNFyfMg@mail.gmail.com> |
| X-Optus-CM-Score | 0 |
| X-Optus-CM-Analysis | v=2.1 cv=bpB1Wiqi c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=5m3_CvCLeBwA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=0Dnk3ZJPZlUA:10 a=pGLkceISAAAA:8 a=rw4YhiA4AAAA:8 a=QIWOR66UefhMjcmSLN4A:9 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A:10 a=1l0VppTa114A:10 |
| 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.7438.1393537221.18130.python-list@python.org> (permalink) |
| Lines | 82 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1393537221 news.xs4all.nl 2913 [2001:888:2000:d::a6]:39928 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:67164 |
Show key headers only | View raw
On 27Feb2014 20:07, Jignesh Sutar <jsutar@gmail.com> 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.
Regexps are quite useful for very variable text. You're just splitting
on ':' and '-:-', which is very easy and does not need a regexp.
Avoid regexps if the code can be written easily and readably without
them; they are cryptic and fragile.
> Notes and code below to demonstrate what I am trying to achieve. Any help,
> much appreciated.
>
> Examples=["Test1A",
> "Test2A: Test2B",
> "Test3A: Test3B -:- Test3C", ""]
Minor remark. Class names tend to have leading uppercase names,
like Foo. ordinary variables tend to have lower case names, like
foo. A habit to get into - it maes your code more readable for
everyone else.
Code suggestions below the code...
> # 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()
I would be parsing the string using "split", progressively.
Example (completely untested):
# initialise all parts because we will only set the parts
# below if they appear in the text
out1, out2, out3 = '', '', ''
words = s.split(':', 1)
if words: # arrays are false if empty, true if non-empty
out1 = words.pop(0)
# there is a second part - split it on "-:-"
if words:
words = words[0].split('-:-', 1)
out2 = words.pop(0)
if words:
out3 = words.pop(0)
# personally I would leave out1 etc as empty strings and only _print_ the word "Empty"
# but if you really want to mangle the variables themselves:
if not out1:
out1 = "Empty"
if not out2:
out2 = "Empty"
if not out3:
out3 = "Empty"
Notice that by using split we do not need to do any funny string
index arithmetic.
Cheers,
--
Cameron Simpson <cs@zip.com.au>
You can't have everything... where would you put it?
- Charles Robinson, cr0100@medtronic.com
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Extracting parts of string between anchor points Cameron Simpson <cs@zip.com.au> - 2014-02-28 08:40 +1100
csiph-web