Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'anyway.': 0.05; 'subject:help': 0.08; 'messing': 0.09; 'surrounded': 0.09; 'trailing': 0.09; 'cc:addr:python-list': 0.11; '_after_': 0.16; 'be:': 0.16; 'dot,': 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; 'simpson': 0.16; 'zero.': 0.16; 'zeroes': 0.16; 'zeros': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'people,': 0.24; 'cheers,': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'suggested': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'leave': 0.29; 'skip:( 20': 0.30; 'decimal': 0.31; 'larry': 0.31; 'strip': 0.31; "they'll": 0.31; 'this.': 0.32; 'figure': 0.32; 'another': 0.32; 'could': 0.34; "can't": 0.35; 'problem.': 0.35; 'but': 0.35; 'add': 0.35; 'charset:us- ascii': 0.36; 'example,': 0.37; 'two': 0.37; 'step': 0.37; 'ends': 0.38; 'aside': 0.39; 'how': 0.40; 'remove': 0.60; 'life,': 0.60; 'negative': 0.60; 'most': 0.60; 'first': 0.61; 'content- disposition:inline': 0.62; "you'll": 0.62; 'to:addr:gmail.com': 0.65; 'jordan,': 0.84; 'received:192.168.15': 0.84; 'sweat': 0.84 Date: Sat, 14 Mar 2015 09:37:51 +1100 From: Cameron Simpson To: Larry Martell Cc: "python-list@python.org" Subject: Re: regex help MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) References: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426286284 news.xs4all.nl 2940 [2001:888:2000:d::a6]:38153 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87391 On 13Mar2015 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. 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. Leaving aside the suggested non-greedy match, you can rephrase this: strip trailing zeroes _after_ the first decimal digit. Then you can consider a number to be: digits point any digit other digits to be right-zero stripped so: (\d+\.\d)(\d*[1-9])?0*\b and keep .group(1) and .group(2) from the match. Another way of considering the problem. Or you could two step it. Strip all trailing zeroes. If the result ends in a dot, add a single zero. Cheers, Cameron Simpson C'mon. Take the plunge. By the time you go through rehab the first time, you'll be surrounded by the most interesting people, and if it takes years off of your life, don't sweat it. They'll be the last ones anyway. - Vinnie Jordan, alt.peeves