scanf style parsing

Malcolm Tredinnick malcolm at commsecure.com.au
Sat Sep 29 18:19:41 CEST 2001


On Sat, Sep 29, 2001 at 03:47:49PM +0000, chasm at atlantis.rift wrote:
> >> Regex's are useful and powerful.  But they're also very easy to
> >> abuse.  I've actually seen the following Perl code:
> >> 
> >>     if ($filename =~ /\.txt$/) { ... }
> >> 
> >> ...
> >>
> >> or, much more preferably:
> >> 
> >>     if filename[-4:] == '.txt':
> >
> >Overall, the Perl code's better.  It didn't have to hard-code the
> >length of the string.
> 
> 	ext = '.txt'
> 	if filename[-len(ext):] == ext:
> 
> 	etc...

Or
	if filename.endswith(ext):
		# etc...

which is the reason the startswith() and endswith() string methods
exist. :-)

Cheers,
Malcolm

-- 
On the other hand, you have different fingers.




More information about the Python-list mailing list