Why re.match()?

Hrvoje Niksic hniksic at xemacs.org
Thu Jul 2 13:55:39 CEST 2009


kj <no.email at please.post> writes:

> For a recovering Perl-head like me it is difficult to understand
> why Python's re module offers both match and search.  Why not just
> use search with a beginning-of-string anchor?

I need re.match when parsing the whole string.  In that case I never
want to search through the string, but process the whole string with
some regulat expression, for example when tokenizing.  For example:

    pos = 0
    while pos != len(s):
        match = TOKEN_RE.match(s, pos)
        if match:
            process_token(match)
            pos = match.end()
        else:
            raise ParseError('invalid syntax at position %d' % pos)



More information about the Python-list mailing list