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


Groups > comp.lang.python > #87380

Re: regex help

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <larry.martell@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'mrab': 0.05; 'subject:help': 0.08; 'messing': 0.09; 'trailing': 0.09; 'search:': 0.16; 'string)': 0.16; 'zeros': 0.16; 'wrote:': 0.18; 'to:name:python-list@python.org': 0.22; "haven't": 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; 'message-id:@mail.gmail.com': 0.30; '13,': 0.31; 'decimal': 0.31; 'larry': 0.31; 'this.': 0.32; 'figure': 0.32; 'thanks!': 0.32; 'fri,': 0.33; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'example,': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'negative': 0.60; 'mar': 0.68; '2015': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=aiKyIXkDNjSFUY8wGG3J7rzvBMCEu2joKtDoylwCN9g=; b=VIIxArd3SsvHL7dzmzqGZW5rGb6a+dzKsGYUINArBz1WXcdofkFyl0aGihBl92QVH3 tFCnFrEYrDSJgnxK0EYlDIANjsNcOM7eoqMVBDbc7NDI396xGwjNIK0vwyIP4KozXqci Asz5s3J6XQlSJ/OieieETjCNOxzGdtNodGBHx93UnPWBOAb0Fp/mCrhGDY2+JAVzMaWv fTIJ9c8XG3DRrEUQE2GbWPhIN1t51+sjucPbue0uULNMnMh1RLlCKqIqrl+I7tYNpsiM aqTgVSiTKeCm/XAMsMp6Gr1ojEJddZ2xmebaB8SALnPMaBuRtNk6rJbfOwLrZcY58K7Y d3qA==
MIME-Version 1.0
X-Received by 10.180.108.81 with SMTP id hi17mr42723197wib.91.1426271931836; Fri, 13 Mar 2015 11:38:51 -0700 (PDT)
In-Reply-To <55031E80.6030904@mrabarnett.plus.com>
References <CACwCsY6cN6+mZVYWnMpKGsXg6jd7Y8Gav9DC7HkXcPtU-pDXDw@mail.gmail.com> <55031E80.6030904@mrabarnett.plus.com>
Date Fri, 13 Mar 2015 14:38:51 -0400
Subject Re: regex help
From Larry Martell <larry.martell@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.335.1426271938.21433.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1426271938 news.xs4all.nl 2850 [2001:888:2000:d::a6]:46793
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87380

Show key headers only | View raw


On Fri, Mar 13, 2015 at 1:29 PM, MRAB <python@mrabarnett.plus.com> wrote:
> On 2015-03-13 16: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. For example, if I have this:
>>
>>
>> 14S,5.0000000000000000,4.56862745000000,3.7272727272727271,3.3947368421052630,5.7307692307692308,5.7547169811320753,4.9423076923076925,5.7884615384615383,5.137254901960000
>>
>> I want to end up with:
>>
>>
>> 14S,5.0,4.56862745,3.7272727272727271,3.394736842105263,5.7307692307692308,5.7547169811320753,4.9423076923076925,5.7884615384615383,5.13725490196
>>
>> I have a regex to remove the zeros:
>>
>> '0+[,$]', ''
>>
>> 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.
>>
> Search: (\.\d+?)0+\b
> Replace: \1
>
> which is:
>
> re.sub(r'(\.\d+?)0+\b', r'\1', string)

Thanks! That works perfectly.

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


Thread

Re: regex help Larry Martell <larry.martell@gmail.com> - 2015-03-13 14:38 -0400

csiph-web