Efficiently test for positive re.match then use the result?

Discussion in 'Python' started by elmlish, Mar 3, 2004.

  1. elmlish

    elmlish Guest

    Hello all,

    I'm currently befuddled as to how to efficiently test for a positive re.
    match then use the results of that match in a function.

    Mostly what I've seen people do is to first test for the match, and then
    try matching again to get the results. This would seem to be pretty
    inefficient to me.

    I've tried making the match, then sending it to a variable, then testing
    if the variable is good and then finally using it, but this still seems
    overkill.

    I'm also trying to use this in list comprehensions, mostly because they
    are kind of fun. What I've got right now looks something like this.


    >>> alist = ['boo','hoo','choo']
    >>> [re.match('choo',line) for line in calist if re.match('choo',line)]

    [<_sre.SRE_Match object at 0x11e218>]

    This is a small test, but what I will be looking for various matches in
    is a large special purpose text file.

    Does anyone have input on how something like this _should_ be done?
    thanks,


    --

    ~elmlish~
     
    elmlish, Mar 3, 2004
    #1
    1. Advertising

  2. On 3 Mar 2004, elmlish <- wrote:

    > Mostly what I've seen people do is to first test for the match, and then
    > try matching again to get the results. This would seem to be pretty
    > inefficient to me.


    Where did you see that? Often you see code like:

    m = re.match('foo', 'foobar')
    if m: do_something with m

    > I've tried making the match, then sending it to a variable, then testing
    > if the variable is good and then finally using it, but this still seems
    > overkill.


    It isn't; you can't have directly an return value from assignment in
    Python like e.g in C, so you can't write code like:

    if m = re.match('foo', 'foobar'): do_somethiing_with_m

    Global vars are also normally not set from re-matching (you could write
    your own matching function which sets a global var; but that's seldom a
    good idea ).

    > I'm also trying to use this in list comprehensions, mostly because they
    > are kind of fun. What I've got right now looks something like this.



    >>>> alist = ['boo','hoo','choo']
    >>>> [re.match('choo',line) for line in calist if re.match('choo',line)]

    > [<_sre.SRE_Match object at 0x11e218>]


    > This is a small test, but what I will be looking for various matches in
    > is a large special purpose text file.


    > Does anyone have input on how something like this _should_ be done?
    > thanks,


    I don't know how it _should_ be done but I can tell you how it _could_ be
    done.

    Use a class like the following:

    class Matcher (object):
    __slots__ = ('reg', 'match')

    def __init__ (self, reg):
    self.reg = reg
    self.match = None

    def __call__ (self, val):
    self.match = self.reg(val)
    if self.match:
    return True

    Now you could use it like:

    >>> alist = ['boo','hoo','choo']
    >>> reg = Matcher(re.compile('choo').match)
    >>> [reg.match for c in alist if reg(c)]

    [<_sre.SRE_Match object at 0xb3de58>]
    >>>


    That's no overkill.

    But if you wanted it even lighter you could use a closure (but don't
    tell anyone :) )

    def matcher (reg):
    res = [None]
    def fun (s):
    m = reg(s)
    if m: res[0] = m
    return m
    return res, fun

    You use it like that:

    >>> res, reg = matcher(re.compile('choo').match)
    >>> [res[0] for c in alist if reg(c)]

    [<_sre.SRE_Match object at 0xa14e90>]
    >>>



    Or you simply write:

    >>> reg = re.compile('choo').match
    >>> filter(None, [reg(line) for line in alist])

    [<_sre.SRE_Match object at 0xb3df38>]


    Even for a big list filter(None, ...) is fast.




    KP

    --
    You know you've been sitting in front of your Lisp machine too long
    when you go out to the junk food machine and start wondering how to
    make it give you the CADR of Item H so you can get that yummie
    chocolate cupcake that's stuck behind the disgusting vanilla one.
     
    =?iso-8859-1?q?Karl_Pfl=E4sterer?=, Mar 3, 2004
    #2
    1. Advertising

  3. elmlish wrote:
    > I'm currently befuddled as to how to efficiently test for a positive
    > re. match then use the results of that match in a function.


    If you do this often, you might want to define a generator function:

    def reMatches (iterator, patternString):
    rex = re.compile (patternString)
    for text in iterator:
    match = rex.match (text)
    if match:
    yield match

    This can be used as
    for match in reMatches (file (fname), 'href="([^"]*)"'):
    print match.group (0)

    Variations include:
    - reSearch to use the search instead of the match method
    - allow to pass a string or a compiled regular expression as the second
    parameter to reMatches
    - find all the matches in a line and returning each with a separate yield

    Daniel
     
    Daniel Dittmar, Mar 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hicham Mouline
    Replies:
    2
    Views:
    807
    Keith Thompson
    Apr 23, 2010
  2. Replies:
    0
    Views:
    325
  3. Michael Tan
    Replies:
    32
    Views:
    1,048
    Ara.T.Howard
    Jul 21, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    109
    PerlFAQ Server
    Jan 19, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    161
    PerlFAQ Server
    Apr 28, 2011
Loading...

Share This Page