Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'programmer': 0.03; 'exists.': 0.07; 'modified': 0.07; 'though:': 0.07; 'string': 0.09; 'identifier': 0.09; 'valueerror:': 0.09; 'way:': 0.09; 'yeah,': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'assume': 0.14; 'ast': 0.16; 'backslashes': 0.16; 'chris,': 0.16; 'dict': 0.16; 'dictionary.': 0.16; 'finds': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guru': 0.16; 'hmm.': 0.16; 'nodes': 0.16; 'operation,': 0.16; 'ought': 0.16; 'quoted': 0.16; 'regex,': 0.16; 'string:': 0.16; 'subscripting': 0.16; 'through,': 0.16; 'wrote:': 0.18; 'module': 0.19; "python's": 0.19; 'work,': 0.20; 'seems': 0.21; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'this?': 0.23; 'error': 0.23; 'replace': 0.24; 'skip': 0.24; 'skip:e 30': 0.24; 'skip:{ 20': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'source': 0.25; 'help!': 0.26; 'skip:" 20': 0.27; 'skip:_ 20': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'appear': 0.29; 'leave': 0.29; 'raise': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'reply.': 0.31; '"",': 0.31; 'context,': 0.31; 'node': 0.31; 'quotes': 0.31; "skip:' 40": 0.31; 'allows': 0.31; 'file': 0.32; 'this.': 0.32; 'text': 0.33; '(most': 0.33; 'raw': 0.33; 'actual': 0.34; "i'd": 0.34; 'could': 0.34; 'message.': 0.35; 'knows': 0.35; 'something': 0.35; 'case,': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'replies': 0.36; 'surely': 0.36; 'thanks': 0.36; "i'll": 0.36; 'possible': 0.36; 'similar': 0.36; 'wrong': 0.37; 'sometimes': 0.38; 'ahead': 0.38; 'pm,': 0.38; 'previous': 0.38; 'that,': 0.38; 'little': 0.38; 'recent': 0.39; 'explain': 0.39; 'does': 0.39; 'quote': 0.39; 'enough': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'expression': 0.60; 'is.': 0.60; 'most': 0.60; 'extended': 0.61; 'full': 0.61; 'simply': 0.61; 'save': 0.62; "you'll": 0.62; 'making': 0.63; 'provide': 0.64; 'more': 0.64; 'deals': 0.65; 'hours': 0.66; 'answer.': 0.68; 'skip:a 40': 0.72; 'dollar': 0.74; '3.4': 0.84; 'informative,': 0.84; "it'd": 0.84; 'safe.': 0.84; 'thing).': 0.84; 'underneath': 0.84; 'absolutely': 0.87; '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=vIDsaAstIaAXCj/ufWubupD09jJsSsus7p0IbBa3t6c=; b=dQ2/XhiPOEd7Jw2cceYatABjLiZMSUTP5LXlOuRrqwJJ47HTxk5f20BaEGM6W316pt EGzkqlPCnQrCFfW3GfXAAtmVXNeerVEIXwi4YveNVzYX1/nUznydb6CJwOp9BeaWLI9j 74MOJzHFh/5jyNDX2AquV4iSEV+Wh8Uu7ak9/trcOs6XMqnIa/OtYBLFmmGXYfje8LId S7+ZI2iNeew7rieAt63Q9jhE5h9H74jlOmw9M5bEOn/i2T4rNhWGUHg3MyROuESvxjBW ozIaKbhQmeTalj2t0yarMwuKBP8oYyPEmFHQeMZmLMz49WZJ3hxrlIeJwcDwfO6rRYnp DLRQ== MIME-Version: 1.0 X-Received: by 10.50.79.167 with SMTP id k7mr4450864igx.14.1414501171851; Tue, 28 Oct 2014 05:59:31 -0700 (PDT) In-Reply-To: References: <8c1c1b7f-d121-4f3d-adf9-8262ddd8f102@googlegroups.com> Date: Tue, 28 Oct 2014 23:59:31 +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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414501181 news.xs4all.nl 2853 [2001:888:2000:d::a6]:47845 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79839 (Please quote enough of the previous text to provide context, and write your replies underneath the quoted text - don't assume that everyone's read the previous posts. Thanks!) On Tue, Oct 28, 2014 at 11:28 PM, wrote: > Hi Chris, thanks for the reply. I tried to use look ahead assertions, in particular I modified the regex this way: > > newstring = re.sub(ur"""(?u)(\$\"[\s\w(?<=\\)\"]+\")""", subst, oldstring) > > but it does not work. I'm absolutely not a regex guru so I'm surely missing something. Yeah, I'm not a high-flying regex programmer either, so I'll leave the specifics for someone else to answer. Tip, though: Print out your regex, to see if it's really what you think it is. When you get backslashes and quotes coming through, sometimes you can get tangled, even in a raw string literal; sometimes, one quick print(some_re) can save hours of hair-pulling. > The strings I'm dealing with are similar to formulas, let's say something like: > > '$["simple_input"]+$["messed_\\"_input"]+10' > > Thanks for any help! Hmm. This looks like a job for ast.literal_eval with an actual dictionary. All you'd have to do is replace every instance of $ with a dict literal; it mightn't be efficient, but it would be safe. Using Python 2.7.8 as you appear to be on 2.x: >>> expr = '$["simple_input"]+$["messed_\\"_input"]+10' >>> values = {"simple_input":123, "messed_\"_input":75} >>> ast.literal_eval(expr.replace("$",repr(values))) Traceback (most recent call last): File "", line 1, in ast.literal_eval(expr.replace("$",repr(values))) File "C:\Python27\lib\ast.py", line 80, in literal_eval return _convert(node_or_string) File "C:\Python27\lib\ast.py", line 79, in _convert raise ValueError('malformed string') ValueError: malformed string Unfortunately, it doesn't appear to work, as evidenced by the above message. It works with the full (and dangerous) eval, though: >>> eval(expr.replace("$",repr(values))) 208 Can someone who better knows ast.literal_eval() explain what's malformed about this? The error message in 3.4 is a little more informative, but not much more helpful: ValueError: malformed node or string: <_ast.BinOp object at 0x0169BAF0> My best theory is that subscripting isn't allowed, though this seems odd. In any case, it ought in theory to be possible to use Python's own operations on this. You might have to do some manipulation, but it'd mean you can leverage a full expression evaluator that already exists. I'd eyeball the source code for ast.literal_eval() and see about making an extended version that allows the operations you want. If you can use something other than a dollar sign - something that's syntactically an identifier - you'll be able to skip the textual replace() operation, which is risky (might change the wrong thing). Do that, and you could have your own little evaluator that uses the ast module for most of its work, and simply runs a little recursive walker that deals with the nodes as it finds them. ChrisA