perl to python, case + regular expression question

Aahz Maruch aahz at netcom.com
Tue Jul 25 16:46:31 EDT 2000


In article <y7gd7k26jtd.fsf at bandersnatch.lerc.nasa.gov>,
Jim McKim  <mckim at bandersnatch.lerc.nasa.gov> wrote:
>
>if re.search('^log entry (\d+)', input_line):
>    match = re.search('^log entry (\d+)', input_line)
>    log_entry_num = match.group(1)
>    ...etc.

I'd write this as

re_log_entry = re.compile(r'^log entry (\d+)')
    ...
match = re_log_entry.search(input_line)
if match is not None:
  log_entry_num = match.group(1)

Note the use of the raw (r'') string to protect the backslash, the fact
that we're gaining efficiency by not compiling the regex in the loop,
and that check against None.
--
                      --- Aahz (Copyright 2000 by aahz at netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Zie is so far from a clue that it would take a sub-light vessel several
years to reach one.  --Aahz



More information about the Python-list mailing list