re.match -- not greedy?

Paul McGuire ptmcg at austin.rr._bogus_.com
Sun Nov 19 20:32:22 EST 2006


"EXI-Andrews, Jack" <Jack.Andrews at boeing.com> wrote in message 
news:mailman.476.1163979600.32031.python-list at python.org...

in real life, i am trying to match #defines:

>>> re.match( '#define ([A-Za-z0-9_]+)([^(])', '#define
abc(d)').groups()
('ab', 'c')

i want this example to fail because the first character after a string
of letters is a '('
i want to match only #defines without parameters.
>
>

Here's a pyparsing grammar that matches your #defines as you describe (skip 
over #defines with parameters), and also skips over ones that are commented 
out.  It also parses the rest of the definition line (definitions with 
end-of-line '\' continuations not included in this sample):

from pyparsing import *
defWithoutParen = "#define" + \
                Word(alphas,alphanums+"_") + \
                ~Literal("(").leaveWhitespace() + \
                Optional(restOfLine)
defWithoutParen.ignore(cStyleComment)

Here's sample code showing how to use this grammar:

sample = """
#define PI 3.141592
#define SUCCESS (href==1)
#define MIN(a,b) a<b ? a : b
#define E 2.71828
/*
#define lockMutex mockLockMutex
*/
#define WIN32
"""
matches = defWithoutParen.searchString(sample)
for m in matches:
    print m

Prints out:
['#define', 'PI', '3.141592']
['#define', 'SUCCESS', '(href==1)']
['#define', 'E', '2.71828']
['#define', 'WIN32', '']

-- Paul





More information about the Python-list mailing list