Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.05; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:$ 30': 0.09; 'snippet': 0.09; 'python': 0.11; 'advance!': 0.16; 'backslash,': 0.16; 'expressions,': 0.16; 'occurences': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'substitute': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'everyone,': 0.19; 'header:User-Agent:1': 0.23; 'header:X -Complaints-To:1': 0.27; 'correct': 0.29; "i'm": 0.30; 'anyone': 0.31; 'regular': 0.32; 'another': 0.32; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'next': 0.36; 'thanks': 0.36; 'handle': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'here': 0.66; 'skip:r 30': 0.69 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Eugene Subject: Re: Regex substitution trouble Date: Wed, 05 Nov 2014 13:44:35 +0300 References: <8c1c1b7f-d121-4f3d-adf9-8262ddd8f102@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: 213.208.191.85 User-Agent: KNode/4.14.2 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415187006 news.xs4all.nl 2922 [2001:888:2000:d::a6]:54729 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80256 massi_srb@msn.com wrote: > Hi everyone, > > 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? > > Thanks in advance! Hi! Next snippet works for me: re.sub(r'\$"([\s\w]+(\\")*[\s\w]+)+"', 'noop', r'$"te\"sts\"tri\"ng"')