regex matching question

bullockbefriending bard kinch1967 at gmail.com
Sat May 19 19:46:41 EDT 2007


> Backslash? Your example uses a [forward] slash.

correct.. my mistake. i use forward slashes.

> Are you sure you don't want to allow for some spaces in the data, for
> the benefit of the humans, e.g.
>     1,2 / 3,4 / 5,6 / 7,8 / 9,10 / 11,12

you are correct. however, i am using string as a command line option
and can get away without quoting it if there are no optional spaces.

> Always use "raw" strings for patterns, even if you don't have
> backslashes in them -- and this one needs a backslash; see below.

knew this, but had not done so in my code because wanted to use '\' as
a line continuation character to keep everything within 80 columns.
have adopted your advice regarding \Z below and now am using raw
string.

> For clarity, consider using "mobj" or even "m" instead of "match" to
> name the result of re.match.

good point.

> > if match == None or match.group(0) != results:
>
> Instead of
>     if mobj == None ....
> use
>     if mobj is None ...
> or
>     if not mobj ...
>
> Instead of the "or match.group(0) != results" caper, put \Z (*not* $) at
> the end of your pattern:
>     mobj = re.match(r"pattern\Z", results)
>     if not mobj:
>
> HTH,
> John

very helpful advice. thanks!




More information about the Python-list mailing list