Regular Expressions in Python

Paul McGuire ptmcg at users.sourceforge.net
Mon Mar 1 10:52:30 EST 2004


"Jeff Epler" <jepler at unpythonic.net> wrote in message
news:mailman.16.1078152536.22701.python-list at python.org...
> opt_spaces  = " *"
> identifier  = "[A-Za-z_][A-Za-z0-9_]+"
> comment     = "\*.*"
> opt_comment = "(%s)?" % comment
>
> pat = re.compile(opt_spaces + identifier + opt_spaces + opt_comment + "$")
>
> for test in (
>         "  END *This is a comment",
>         "  END  e * This is a line with an error (e)"):
>     print test, pat.match(test)
>
> Jeff
>
Assuming you're more interested in the identifier than in the comment,
change identifier to "([A-Za-z_][A-Za-z0-9_]+)" so that the keyword gets
saved in the pat.match.groups() list.

-- Paul





More information about the Python-list mailing list