matching multiple regexs to a single line...

Alexander Sendzimir lists at battleface.com
Wed Nov 20 21:35:28 EST 2002


So, with the help of those that responded, I'm posting my preferred
solution to the original question of this thread which is, "how does one
apply multiple regexs to a single line as in a compiler in Python?"

There are several ways of solving this problem and each has its merits.
Please see my NOTE AT END about this.

Here's the generalized version:

<code>

import re

#
# define handlers for each matching pattern:
#

def reHandler1 ( matchObject ) :
    # do something with matchObject

def reHandler2 ( matchObject ) :
    # do something with matchObject

  .
  .
  .

def reHandlerN ( matchObject ) :
    # do something with matchObject


#
# associate the regular expressions with their handlers
# in a list of tuples (pairs). The order of the pairs is
# the order in which matching will take place. Like lex.
#

regexs = [
    ( re.compile( r'...' ), reHandler1 ),
    ( re.compile( r'...' ), reHandler2 ),
      .
      .
      .
    ( re.compile( r'...' ), reHandlerN ) ]


#
# now, match each regular expression in turn against the current line.
# When a match is found, break to the next line. Lines comes from
# where ever you want it to.
#

for line in lines :
    for ( regex, handler ) in regexs :
        mo = regex.match( line )
        if mo :
            handler( mo )
            break
        else :
            # did not match this expression
            pass

</code>



NOTE AT END

There are a number of different ways of solving this problem. Several have
been discussed in this thread. This is my preferred approach because it's
clean and simple in my opinion and makes use of a minimal knowledge
foundation. I like the fact that it is made up of only three parts: [1] the
handler definitions, [2] the regexs list, and [3] the matching loops.
Finally, each of these parts is consistent and relatively easy to understand.
I'm posting this solution because I feel it might be of use to others whether
new or experienced since I haven't been able to find any relevant information
on this topic in this news group.





More information about the Python-list mailing list