problem with split

MonkeeSage MonkeeSage at gmail.com
Sat Oct 7 02:09:08 EDT 2006



On Oct 6, 11:33 pm, hanumizzle <hanumiz... at gmail.com> wrote:
> import re
>
> <snip>
>
> if line.startswith('instr'):
>   p = re.compile(r'(\d+)\s+;(.*)$')
>   m = p.search(line)
>
> return (m.group(1), m.group(2))

You probably don't want startswith, in case there are initial spaces in
the line. Also, since the regexp is single use, you can just use the
re.search class method, which will compile the regexp implicitly. May
also want to strip the second grouped match, in case of trailing
spaces.

if 'instr' in line:
  m = re.search(r'(\d+)\s+;(.*)$', line)
  if m:
    return (m.group(1), m.group(2).strip())

Regards,
Jordan




More information about the Python-list mailing list