Negative look-behind

From: Bhargava (bhargava78_at_yahoo.com)
Date: 06/01/04


Date: 1 Jun 2004 02:14:54 -0700

Hello,

I am a newbie to python and need some help.

I am looking at doing some batch search/replace for some of my source
code. Criteria is to find all literal strings and wrap them up with
some macro, say MC. For ex., var = "somestring" would become var =
MC("somestring"). Literal strings can contain escaped " & \.

But there are 2 cases when this replace should not happen:
1.literal strings which have already been wrapped, like
MC("somestring")
2.directives like #include "header.h" and #extern "C".

I tried to use negative look-behind assertion for this purpose. The
expression I use for matching a literal string is
"((\\")|[^"(\\")])+". This works fine. But as I start prepending
look-behind patterns, things go wrong. The question I have is whether
the pattern in negative look-behind part can contain alternation ? In
other words can I make up a regexp which says "match this pattern x
only if it not preceded by anyone of pattern a, pattern b and pattern
c" ?

I tried the following expression to take into account the two
constraints mentioned above, (?<![(#include )(#extern
)(MC\()])"((\\")|[^"(\\")])+". Can someone point out the mistakes in
this ?

Thanks,
Bhargava



Relevant Pages

  • Re: Negative look-behind
    ... Criteria is to find all literal strings and wrap them up with ... >> only if it not preceded by anyone of pattern a, ... > transformStringmethod to pyparsing; ...
    (comp.lang.python)
  • Re: Using "new function() {...}" as a Singleton
    ... in a 1 to 1 pattern "challenge" it was worth it to mention that: ... The reason I did not mention is that a singleton is usually complex ... up problem (I still use this pattern to preserve an extra "var" when ... On NewExpression, I still believe it's a valid option, somehow more ...
    (comp.lang.javascript)
  • Re: Problem in bin/sh stripping the * character through ${expansion%}
    ... Other "special" characters do work if they are ... var before stripping: foo\* ... '*' when treated as the smallest pattern, ...
    (freebsd-hackers)
  • Re: Using "new function() {...}" as a Singleton
    ... new expression is basically an "elegant" module pattern recognizable ... var o = new function; ... About minifiers, mungers, obfuscators, etc, the module pattern wins ... var self = this; ...
    (comp.lang.javascript)
  • Re: function "attributes"
    ... You want the "Strategy" pattern, making the function into an object: ... class TestFunction ... var $probability = 0.234321; ...
    (comp.lang.php)