matching multiple regexs to a single line...

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Nov 22 01:51:16 EST 2002


>>>>> "Alex" == Alex Martelli <aleax at aleax.it> writes:

    Alex> So, if your question was meant to be rhetorical, it seems
    Alex> particularly inappropriate to me.  If instead it was asked
    Alex> in earnest, to learn crucial facts you didn't know about how
    Alex> re's and mo's work or about how they're often used, then I
    Alex> think you might usefully have refrained from criticizing
    Alex> what you suspected you didn't fully understand.  But I do
    Alex> gather that trying to understand a subject before
    Alex> criticising is quite an obsolete approach these days.

I, for one, am interested in learning crucial facts.  

For me, the launching point of this thread was on "differential
processing of different match objects".  That is, if a string matches
rgx1 (with it's own particular subgroups), do process1.  If it matches
rgx2, with a (possibly) different set of subgroups, do process2.

   http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=mailman.1037141776.20667.python-list%40python.org

That the match object processing functions in the example code in
previous posts did not explicitly process match objects differently
was a time and typing convenience, as illustrated in the comment
string of the example below:
(http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=mailman.1037751635.2665.python-list%40python.org):

   handler1 = handler2 = handler3 = lambda x: None   # some very dumb handlers

That said, I too am unsatisfied with the M*N performance of the (rgx,
func) pattern:

  for line in lines[:M]:
     for rgx in regexs[:N]:
         mo = rgx.match(line)
         if mo: do_something(mo)

You've mentioned named rgx's in your previous posts.  In the case of
differential processing of match objects based on the regex match, is
there a more efficient way to process the mo's than this M*N approach?

Aside from these details, let me just say that you (Alex) rock.
Thanks for your posts, your recipes, and all that.

John Hunter






More information about the Python-list mailing list