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


Groups > comp.lang.python > #87379

Re: regex help

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'subject:help': 0.08; "'.'": 0.09; 'messing': 0.09; 'trailing': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "'0'": 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'personally,': 0.16; 'zeros': 0.16; 'wrote:': 0.18; 'input': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; "haven't": 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply- To:1': 0.27; 'leave': 0.29; "skip:' 10": 0.31; 'decimal': 0.31; 'larry': 0.31; 'this.': 0.32; 'figure': 0.32; "can't": 0.35; 'but': 0.35; 'charset:us-ascii': 0.36; 'received:10': 0.37; 'version,': 0.38; 'how': 0.40; 'remove': 0.60; 'negative': 0.60; 'you.': 0.62; 'to:addr:gmail.com': 0.65; 'received:46': 0.66; 'suits': 0.84
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1426268240960:3721671881
X-MC-Ingress-Time 1426268240959
Date Fri, 13 Mar 2015 12:38:38 -0500
From Tim Chase <python.list@tim.thechases.com>
To Larry Martell <larry.martell@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Subject Re: regex help
In-Reply-To <CACwCsY6cN6+mZVYWnMpKGsXg6jd7Y8Gav9DC7HkXcPtU-pDXDw@mail.gmail.com>
References <CACwCsY6cN6+mZVYWnMpKGsXg6jd7Y8Gav9DC7HkXcPtU-pDXDw@mail.gmail.com>
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-AuthUser tim@thechases.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
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.334.1426268765.21433.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1426268765 news.xs4all.nl 2930 [2001:888:2000:d::a6]:32776
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87379

Show key headers only | View raw


On 2015-03-13 12:05, Larry Martell wrote:
> I need to remove all trailing zeros to the right of the decimal
> point, but leave one zero if it's whole number. 
> 
> But I can't figure out how to get the 5.0000000000000000 to be 5.0.
> I've been messing with the negative lookbehind, but I haven't found
> one that works for this.

You can do it with string-ops, or you can resort to regexp.
Personally, I like the clarity of the string-ops version, but use
what suits you.

-tkc

import re
input = [
'14S',
'5.0000000000000000',
'4.56862745000000',
'3.7272727272727271',
'3.3947368421052630',
'5.7307692307692308',
'5.7547169811320753',
'4.9423076923076925',
'5.7884615384615383',
'5.137254901960000',
]

output = [
'14S',
'5.0',
'4.56862745',
'3.7272727272727271',
'3.394736842105263',
'5.7307692307692308',
'5.7547169811320753',
'4.9423076923076925',
'5.7884615384615383',
'5.13725490196',
]


def fn1(s):
    if '.' in s:
        s = s.rstrip('0')
        if s.endswith('.'):
            s += '0'
    return s

def fn2(s):
    return re.sub(r'(\.\d+?)0+$', r'\1', s)

for fn in (fn1, fn2):
    for i, o in zip(input, output):
        v = fn(i)
        print "%s: %s -> %s [%s]" % (v == o, i, v, o)

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


Thread

Re: regex help Tim Chase <python.list@tim.thechases.com> - 2015-03-13 12:38 -0500

csiph-web