How do I match the last occurence of a character?

Steve Halliburton shallib at netscape.com
Thu Jun 10 05:39:44 CEST 1999


Why not just use the os module:

>>> import os
>>> name = 'news/breakingnews/981001b.html'
>>> base = os.path.split(name)[0]
>>> base
'news/breakingnews'
>>> file = os.basename(name)
>>> file
'981001b.html'


Steve H.

Blake Winton wrote:

> On Thu, 10 Jun 1999, Jeff P. <jeffrey.polaski at west.boeing.com> wrote:
> >How do I match the last occurence of '\' in a string?
> >import regex
>
> This is your first problem.  Why use a tool more powerful than you
> really want?  Check out the string module.
>
> >>> import string
> >>> name = "test\\a\\b\\c.html"
> >>> string.rfind(name,"\\")
> 8
> >>> name2 = name[0:string.rfind(name,"\\" )]
> >>> name2
> 'test\\a\\b'
>
> Note that I have to use "\\" to match a single "\".
> Oh, and to get output similar to the one in your example, you would have
> to use something like
>
> baseurl = url[ 0 : string.rfind( url, "\\" )+1 ]





More information about the Python-list mailing list