Negative look-behind

Paul McGuire ptmcg at austin.rr._bogus_.com
Sat Jun 5 10:30:49 CEST 2004


"Bhargava" <bhargava78 at yahoo.com> wrote in message
news:e7283ab0.0406010114.51ae8b6b at posting.google.com...
> 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

Please check out the latest beta release of pyparsing, at
http://pyparsing.sourceforge.net .  Your post inspired me to add the
transformString() method to pyparsing; look at the included scanExamples.py
program for some search-and-replace examples similar to the ones you give in
your post.

Sincerely,
-- Paul McGuire





More information about the Python-list mailing list