How do I match the last occurence of a character?

Blake Winton bwinton at tor.dhs.org
Wed Jun 9 23:23:06 EDT 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?
>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 ]

Hope this helps,
Blake.

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




More information about the Python-list mailing list