Problems with regexps

From: Kirk Strauser (kirk_at_daycos.com)
Date: 11/07/03


Date: Fri, 07 Nov 2003 21:20:08 GMT

I'm writing a program to scan through a bunch of VB source.  An example of
one of the types of strings I'm trying to match is:

    response.write Request.Cookies ("domain")("cname")

but I do not want to match variable assignments like this (they are picked
up later by a different pattern):

    strXRSCust= Request.Cookies ("domain")("cname")

I'm differentiating between the two forms by looking for an equal sign
followed by zero or more spaces; if the '=' is there, then I don't want to
match.  Here's where it gets weird.  This pattern works perfectly (as long
as there are 1 or more spaces after the '='), in that it will not match
the assignment example above:

    re.compile(r'(?<!=)\s+Request.Cookies\s*((\(\s*".*?"\s*\)\s*)+)')

I really want to use the pattern below to match for zero or more spaces
(not one or more).  Note that it's identical except that the first '\s+'
is replaced with a '\s*':

    re.compile(r'(?<!=)\s*Request.Cookies\s*((\(\s*".*?"\s*\)\s*)+)')

I don't know why, but the second pattern does match the assignment example
above, although I don't think it should.

It seems like there's a problem with the negative lookbehind assertion and
that the variable-length '\s' pattern immediately following it is throwing
it off.  Any thoughts?

-- 
Kirk Strauser
The Day Companies


Relevant Pages

  • Using Integers and Bit Manipulation
    ... can I do an assignment to iy without error? ... do simple assignments for the purpose of copying the bit pattern to ... pattern that doesn't fit the compiler/computers rules for a valid ...
    (comp.lang.fortran)
  • Re: Is this C program doing what it is supposed to do ?
    ... The pattern is the same. ... I've been mentioning all the time that my objection is to the general ... I'd probably write the assignment as a separate ...
    (comp.lang.c)
  • Re: Design: Custom Exception Usage
    ... It'd be silly to call a method that basically has to do the work of parsing the string, but which only returns a flag telling me if actually parsing the string will work. ... Beyond that, however, since sometimes performance _is_ an issue, it's not like TryParse() could have been left out of the API. ... That provides the exact assignment pattern you're asking for. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is this C program doing what it is supposed to do ?
    ... The pattern is the same. ... If you object to the assignment in the conditional test, ... I will make an exception for a loop. ...
    (comp.lang.c)
  • Re: [KSH] Assign a matched pattern to a shell variable
    ... > portion of the string that the pattern matched. ... but matched against the string: ... > the assignment to extract a substring from $. ...
    (comp.unix.shell)