Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'string.': 0.05; 'string': 0.09; 'skip:$ 30': 0.09; 'yeah,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'suggest': 0.14; 'backslash': 0.16; 'backslash,': 0.16; 'expressions,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'occurences': 0.16; 'ought': 0.16; 'problem!': 0.16; 'regex,': 0.16; 'substitute': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'that.': 0.31; 'anyone': 0.31; 'file': 0.32; 'regular': 0.32; 'another': 0.32; 'text': 0.33; 'could': 0.34; 'problem.': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'possible': 0.36; 'handle': 0.38; 'pm,': 0.38; 'that,': 0.38; 'anything': 0.39; 'itself': 0.39; 'sure': 0.39; 'such': 0.63; 'networking': 0.64; 'different': 0.65; 'here': 0.66; 'surrounding': 0.68; 'skip:r 30': 0.69; 'legal': 0.71; 'loose': 0.84; 'presumably': 0.84; 'to:none': 0.92 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:cc :content-type; bh=y+fEyib7gWroidtgDDpykaFbPvkCw4qVzuPvz9poME4=; b=r0VLShMFAS2jLulOpFrSkbEQVGTJ23V78ndzh0GMsknpKIjvKV2vdPcl5hsJUiRy3+ nn0HYCIvtG6MPFFdVSkTbaqPqhI4f1gZwUJ67zlVveGOdRTKEfae2avvHV8g643Aj42A wdhcQ8E3AwHWRT1+E/qiZYSMEw+wvxkVYW09w2cat+v5xZaAZIt4R0Kuqe1p5syT7Xai YFKcLb8J6tU+NaLIdopAsGlmh/YSA5/MvCUfekEVusKUApiAlD5MaKfu7uc/lj0N55K6 TXaywaauRTcOBDNWOAywGrvIvzXrsubd/HU1d2ZCYZjVnDqubpCVWOiq51V4sZdB8WFp I9nA== MIME-Version: 1.0 X-Received: by 10.43.66.193 with SMTP id xr1mr3033395icb.59.1414495482542; Tue, 28 Oct 2014 04:24:42 -0700 (PDT) In-Reply-To: <8c1c1b7f-d121-4f3d-adf9-8262ddd8f102@googlegroups.com> References: <8c1c1b7f-d121-4f3d-adf9-8262ddd8f102@googlegroups.com> Date: Tue, 28 Oct 2014 22:24:42 +1100 Subject: Re: Regex substitution trouble From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414495491 news.xs4all.nl 2972 [2001:888:2000:d::a6]:32859 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:79835 On Tue, Oct 28, 2014 at 10:02 PM, 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 :-) Yeah, that sort of thing is perfectly welcome here. Same with questions about networking in Python, or file I/O in Python, or anything like that. Not a problem! > 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? But this is a problem. You can use look-ahead assertions and such to allow the string \" inside your search string, but presumably the backslash ought itself to be escapable, in order to make it possible to have a loose backslash legal at the end of the string. I suggest that, instead of a regex, you look for a different way of parsing. What's the surrounding text like? ChrisA