Problems with regexps

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kirk Strauser

    Problems with regexps

    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")("cna me")

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

        strXRSCust= Request.Cookies ("domain")("cna me")

    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
  • Kirk Strauser

    #2
    Re: Problems with regexps

    OK, let me give a clearer demonstration. Given this program:

    #!/usr/bin/env python

    import re

    sample = 'FOO= BAR'

    if re.search(r'[^=]\s+BAR', sample):
    print 'Match 1.'

    if re.search(r'[^=]\s*BAR', sample):
    print 'Match 2.'

    When run, it produces "Match 2.". Why? I want to match:

    1) Anything but '=', followed by
    2) Zero or more spaces, followed by
    3) 'BAR'

    The first pattern correct does *not* match. Why doesn't the '= ' get struck
    down by the '[^=]' pattern before a '\s+' but not before '\s*'?

    Thanks,
    --
    Kirk Strauser
    The Day Companies

    Comment

    • Dennis Reinhardt

      #3
      Re: Problems with regexps

      > sample = 'FOO= BAR'[color=blue]
      > if re.search(r'[^=]\s*BAR', sample):
      > print 'Match 2.'[/color]

      The [^=] will match on the space preceding BAR. The \s* matches because
      there are zero spaces remaining. Result: match.

      Prediction: you can get the first expression to fail by putting in two
      spaces, not one.

      Caveat: untested.
      --

      Dennis Reinhardt

      DennisR@dair.co m http://www.spamai.com?ng_py


      Comment

      • Ben Finney

        #4
        Re: Problems with regexps

        On Fri, 07 Nov 2003 23:00:34 GMT, Kirk Strauser wrote:[color=blue]
        > sample = 'FOO= BAR'
        >
        > if re.search(r'[^=]\s+BAR', sample):
        > print 'Match 1.'
        >
        > if re.search(r'[^=]\s*BAR', sample):
        > print 'Match 2.'
        >
        > When run, it produces "Match 2.". Why? I want to match:
        >
        > 1) Anything but '=', followed by
        > 2) Zero or more spaces, followed by
        > 3) 'BAR'[/color]

        To understand the answer, you must understand the question.

        If you inspect the MatchObject returned from re.search(), you'll learn
        more about what's going on:
        [color=blue][color=green][color=darkred]
        >>> import re
        >>> sample = 'FOO= BAR'
        >>> match = re.search( r'[^=]\s+BAR', sample )
        >>> match[/color][/color][/color]

        The first match returns the None object.
        [color=blue][color=green][color=darkred]
        >>> match = re.search( r'[^=]\s*BAR', sample )
        >>> match[/color][/color][/color]
        <_sre.SRE_Mat ch object at 0x4021b090>

        The second match returns a Match object; let's see what it matched.
        [color=blue][color=green][color=darkred]
        >>> match.group(0)[/color][/color][/color]
        ' BAR'[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        The matching string contains the space ("Anything but '='"), followed by
        nothing ("Zero or more spaces"), followed by BAR. Exactly what you
        asked for.

        --
        \ "It is seldom that liberty of any kind is lost all at once." |
        `\ -- David Hume |
        _o__) |
        Ben Finney <http://bignose.squidly .org/>

        Comment

        • Paul Foley

          #5
          Re: Problems with regexps

          On Fri, 07 Nov 2003 21:20:08 GMT, Kirk Strauser wrote:
          [color=blue]
          > 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:[/color]
          [color=blue]
          >     response.write Request.Cookies ("domain")("cna me")[/color]
          [color=blue]
          > but I do not want to match variable assignments like this (they are picked
          > up later by a different pattern):[/color]
          [color=blue]
          >     strXRSCust= Request.Cookies ("domain")("cna me")[/color]

          Try http://weitz.de/regex-coach/

          --
          Cogito ergo I'm right and you're wrong. -- Blair Houghton

          (setq reply-to
          (concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz> "))

          Comment

          • Edward K. Ream

            #6
            Re: Problems with regexps

            I've just added script-based find-changed commands to Leo 4.1 beta 1. So
            Leo can execute a script to find the next match, and optionally executes a
            script to do the replacement. This gives Leo the full power of languages
            like Icon or Snobol, using the plain Python language. There is a dead
            simple way for the find script to communicate with the change script, and
            for the find script to continue or not in the case of the Find All or Change
            All commands.

            BTW, scripts could search anywhere, not just in a Leo outline. For
            example, the find script could pull in the found text from any file to any
            Leo node (creating an outline node if you like). Etc.

            I'm very excited about this new feature. At long last we are not limited to
            non-extensible, wimpy tools like re. Fully interactive if you want; fully
            batch if you want. Undo is easy, etc. The sky is the limit, and when you
            are done, you will be able to understand the scripts you wrote. End of re.
            Yeah!

            Edward
            --------------------------------------------------------------------
            Edward K. Ream email: edreamleo@chart er.net
            Leo: Literate Editor with Outlines
            Leo: http://webpages.charter.net/edreamleo/front.html
            --------------------------------------------------------------------


            Comment

            • Edward K. Ream

              #7
              Re: Problems with regexps

              Actually, I misspoke a little. Script-find is fully compatible with re,
              because re can appear in any script. Anyway, there is nothing particularly
              wrong with re for simple tasks. So it's not the end of re; it's the end of
              re for complex tasks for which re just isn't suited.

              Edward
              --------------------------------------------------------------------
              Edward K. Ream email: edreamleo@chart er.net
              Leo: Literate Editor with Outlines
              Leo: http://webpages.charter.net/edreamleo/front.html
              --------------------------------------------------------------------


              Comment

              Working...