Need help capturing output of re.search

John Machin sjmachin at lexicon.net
Fri Jun 27 03:29:09 CEST 2008


On Jun 27, 10:45 am, joemacbusin... at yahoo.com wrote:
> On Jun 26, 5:12 pm, John Machin <sjmac... at lexicon.net> wrote:
>
>
>
> > On Jun 27, 10:01 am, joemacbusin... at yahoo.com wrote:
>
> > > >You may like to read this:http://www.amk.ca/python/howto/regex/
>
> > > This is a good resource.  Thank you.
> > > Someone else pointed out that I needed to change the
>
> > > if reCheck == "None":
>
> > > to
>
> > > if reCheck == None:   # removed the "s
>
> > "Somebody else" should indeed remain anonymous if they told you that.
> > Use
> >    if reCheck is None:
> > or even better:
> >    if not reCheck:
>
> > It's not obvious from your response if you got these points:
> > (1) re.match, not re.search
> > (2) filename.startswith does your job simply and more understandably
>
> Understood.  I replaced re.search with re.match (although both work)

Not so. Consider a filename that starts with some non-alphanumeric
characters followed by CC_ e.g. "---CC_foo". re.search will score a
hit but re.match won't. It's quite simple: if you want hits only at
the beginning of the string, use re.match. Please don't say "I don't
have any filenames like that, so it doesn't matter" ... it's like
saying "They couldn't hit an elephant at this distance".

> can filename.startswith be used in a conditional?  ala:
>
> if filename.startswith('CC_'):
>     processtheFile()

Of course. *Any* expression can be used in a condition. This one
returns True or False -- a prime candidate for such use. Why are you
asking?



More information about the Python-list mailing list