Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'modified': 0.05; 'test,': 0.05; 'subject:help': 0.07; 'python': 0.09; '(it': 0.09; 'minus': 0.09; 'subject:skip:m 10': 0.09; 'subject:string': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; '-tkc': 0.16; 'caveat': 0.16; 'expected,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'message-id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'sign,': 0.16; 'wrote:': 0.17; 'passes': 0.17; 'tests': 0.18; 'followed': 0.20; 'import': 0.21; 'posted': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'skip:( 20': 0.28; 'assert': 0.29; 'that.': 0.30; 'print': 0.32; 'version': 0.34; 'skip:. 20': 0.35; 'expected': 0.35; 'subject:?': 0.35; 'characters': 0.36; 'two': 0.37; 'being': 0.37; 'subject:: ': 0.38; 'skip:( 30': 0.38; 'remove': 0.61; 'subject:Need': 0.61; 'times': 0.63; 'subject:. ': 0.66; 'as:': 0.75; 'frank': 0.75; 'received:50.22': 0.84 Date: Sat, 05 Jan 2013 11:24:13 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: Sia Subject: Re: Need a specific sort of string modification. Can someone help? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357406586 news.xs4all.nl 6877 [2001:888:2000:d::a6]:49228 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36196 On 01/05/13 02:35, Sia wrote: > I have strings such as: > > tA.-2AG.-2AG,-2ag > or > .+3ACG.+5CAACG.+3ACG.+3ACG > > The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign, the number after it and remove i characters after that. So the two strings above become: > > tA.., > and > ... With the same caveat as Frank posted about the second one being "...." (4 dots), I don't know how this version times out: import re r = re.compile(r"[-+](\d+)([^-+]*)") def modify(m): result = m.group(2)[int(m.group(1)):] return result for test, expected in ( ("tA.-2AG.-2AG,-2ag", "tA..,"), (".+3ACG.+5CAACG.+3ACG.+3ACG", "...."), ): s = r.sub(modify, test) print "%r -> %r (%r)" % ( test, s, expected ) assert s == expected, "Nope" (it passes the tests as modified to "....") -tkc