[Tutor] Slicing Question

Paul Tremblay phthenry@earthlink.net
Wed Apr 9 22:53:01 2003


Good point. But personally I like Jeff Shanon's suggestion that you use
os.path, a nifty and powerful module for doing exactly what the origial
poster wanted.

Paul

On Wed, Apr 09, 2003 at 10:47:03PM +0200, Francois Granger wrote:
> 
> At 13:17 -0400 09/04/2003, in message Re: [Tutor] Slicing Question, 
> Jay Dorsey wrote:
> >Brian Christopher Robinson wrote:
> >>I wrote this line of code today:
> >>
> >>if fileName[:-5][-4:].lower() == "test":
> >>
> >>What it does it take a file name that I already know ends in 
> >>".java", cut off the ".java" part, and see if the last 4 letters 
> >>are test.  I was wondering if there's a simpler way of doing this?
> >>
> >
> >Alternatively, you could use a regular expression
> >
> > >>> x = "blahblahtest.java"
> > >>> import re
> > >>> reFilename = re.compile(".+test.java", re.I)
> >>>> if reFilename.search(x):
> >...     print "there it is"
> >...
> >there it is
> 
> This one will catch "blahblahtestejava.java"
> 
> a better one would be: ".+test\.java$"
> \ to escape the dot, and $ for end of string.
> 
> -- 
> Hofstadter's Law :
> It always takes longer than you expect, even when you take into 
> account Hofstadter's Law.
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 

************************
*Paul Tremblay         *
*phthenry@earthlink.net*
************************