How do I match the last occurence of a character?
bwinton at tor.dhs.org
Thu Jun 10 05:23:06 CEST 1999
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?
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"
>>> name2 = name[0:string.rfind(name,"\\" )]
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