Problems with regexps

K

Kirk Strauser

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?
 
K

Kirk Strauser

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,
 
B

Ben Finney

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'

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:
>>> import re
>>> sample = 'FOO= BAR'
>>> match = re.search( r'[^=]\s+BAR', sample )
>>> match

The first match returns the None object.
>>> match = re.search( r'[^=]\s*BAR', sample )
>>> match
<_sre.SRE_Match object at 0x4021b090>

The second match returns a Match object; let's see what it matched.

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

Dennis Reinhardt

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

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.
 
E

Edward K. Ream

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
 
E

Edward K. Ream

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top