Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.05; 'string': 0.09; 'skip:$ 30': 0.09; 'skip:\\ 10': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'backslash': 0.16; 'backslash,': 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; 'occurences': 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; 'simpson': 0.16; 'string:': 0.16; 'substitute': 0.16; 'tillich': 0.16; 'followed': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'have:': 0.19; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'paul': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'character': 0.29; "i'm": 0.30; 'fixing': 0.31; 'quotes': 0.31; 'anyone': 0.31; 'file': 0.32; 'regular': 0.32; 'another': 0.32; 'raw': 0.33; "i'd": 0.34; 'could': 0.34; 'created': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'definition': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'choosing': 0.36; 'received:com.au': 0.36; 'skip:> 10': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'being': 0.38; 'received:211': 0.38; 'handle': 0.38; 'quote': 0.39; 'sure': 0.39; 'content-disposition:inline': 0.62; 'making': 0.63; 'kind': 0.63; 'such': 0.63; 'more': 0.64; 'skip:r 30': 0.69; 'alone.': 0.84; 'pain': 0.84; 'regexp': 0.84; 'skip:/ 30': 0.84; 'to:addr:msn.com': 0.84; 'treating': 0.84 Date: Wed, 29 Oct 2014 13:14:54 +1100 From: Cameron Simpson To: massi_srb@msn.com Subject: Re: Regex substitution trouble MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <8c1c1b7f-d121-4f3d-adf9-8262ddd8f102@googlegroups.com> User-Agent: Mutt/1.5.21 (2010-09-15) References: <8c1c1b7f-d121-4f3d-adf9-8262ddd8f102@googlegroups.com> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=dMCfxopb c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=r5UFkecV8NkA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=AAvYLDLOAAAA:8 a=PxS8cUwLv6JlZg2BHU4A:9 a=CjuIK1q_8ugA:10 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414548909 news.xs4all.nl 2938 [2001:888:2000:d::a6]:49502 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79868 On 28Oct2014 04:02, massi_srb@msn.com wrote: >I'm not really sure if this is the right place to ask about regular >expressions, but since I'm usin python I thought I could give a try :-) >Here is the problem, I'm trying to write a regex in order to substitute all the occurences in the form $"somechars" with another string. This is what I wrote: > >newstring = re.sub(ur"""(?u)(\$\"[\s\w]+\")""", subst, oldstring) > >This works pretty well, but it has a problem, I would need it also to handle the case in which the internal string contains the double quotes, but only if preceeded by a backslash, that is something like $"somechars_with\\"doublequotes". >Can anyone help me to correct it? People seem to be making this harder than it should be. I'd just be fixing up your definition of what's inside the quotes. There seem to be 3 kinds of things: - not a double quote or backslash - a backslash followed by a double quote - a backslash followed by not a double quote Kind 3 is a policy call - take the following character or not? I would go with treating it like kind 2 myself. So you have: 1 [^\\"] 2 \\" 3 \\[^"] and fold 2 and 3 into: 2+3 \\. So your regexp inner becomes: ([^\\"]|\\.)* and the whole thing becomes: \$"(([^\\"]|\\.)*)" and as a raw string: ur'\$"(([^\\"]|\\.)*)"' choosing single quotes to be more readable given the double quotes in the regexp. Cheers, Cameron Simpson -- cat: /Users/cameron/rc/mail/signature.: No such file or directory Language... has created the word "loneliness" to express the pain of being alone. And it has created the word "solitude" to express the glory of being alone. - Paul Johannes Tillich