How do I match the last occurence of a character?

Blake Winton bwinton at
Thu Jun 10 05:23:06 CEST 1999

On Thu, 10 Jun 1999, Jeff P. <jeffrey.polaski at> 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,"\\")
>>> name2 = name[0:string.rfind(name,"\\" )]
>>> name2

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 ]

Hope this helps,

One Will.  One Dream.  One Truth.  One Destiny.  One Love.

More information about the Python-list mailing list