That horrible regexp idiom

Johann C. Rocholl johann at rocholl.net
Thu Feb 10 14:37:52 EST 2005


Hi,

> import re
> foo_pattern = re.compile('foo')
> 
> '>>> m = foo_pattern.search(subject)
> '>>> if m:
> '>>>    pass
> '>>> else:
> '>>>    pass

I agree that is horrible. This is one of my favorite problems with
python syntax.

> but it occured to me today, that it is possible to do it in python
> without the extra line.
> '
> '>>> def xsearch(pattern, subject):
> '>>>     yield pattern.search(subject)
> 
> '>>> for m in xsearch(foo_pattern, subject):
> '>>>     pass
> '>>> else:
> '>>>     pass

I think I wouldd rather have it this way, based on a suggestion by
Diez B. Roggisch recently:

import re

class matcher:
    def __init__(self, regex):
        self.regex = re.compile(regex)
    def match(self, s):
        self.m = self.regex.match(s)
        return not self.m is None
    def search(self, s):
        self.m = self.regex.search(s)
        return not self.m is None
    def group(self, n = None):
        if n is None:
            return self.m.group()
        return self.m.group(n)

m = matcher('(foo)(.*)')
if m.match('foobar'):
    print m.group()
if m.search('barfoobaz'):
    print m.group(2)

I think one solution that does not need a wrapper class would be to
add the group() method to the match objects from module 're'. IIRC,
the obsolete package 'regex' provided this, once upon a time.

Cheers,
Johann



More information about the Python-list mailing list