[Tutor] string fomatting

Kent Johnson kent37 at tds.net
Sat Jan 31 20:51:00 CET 2009


On Sat, Jan 31, 2009 at 1:44 PM, Jay Jesus Amorin <jay.amorin at gmail.com> wrote:
> Thanks bob.
>
> I want to search any characters in test after https://www.localhost.org/ and
> the search will end after it finds another /
>
> and when i print it will display testmodule.

One way to do this would be to use urlparse.urlparse() to split the
URL into parts, then reassemble the bits you want. Alternately, you
can use a regular expression to split it up:

In [1]: import re

In [2]: test = "https://www.localhost.org/testmodule/dev/trunk/admin/sql/mytest.sql"

This re finds everything up to the third / as group 1, everything
after the third / as group3:
In [10]: match = re.search(r'(http(s)?://[^/]+/)(.*)', test)

In [11]: match.group(1)
Out[11]: 'https://www.localhost.org/'

In [12]: match.group(3)
Out[12]: 'testmodule/dev/trunk/admin/sql/mytest.sql'

You can now split on / to get the first path element:
In [13]: match.group(3).split('/')[0]
Out[13]: 'testmodule'

Kent


More information about the Tutor mailing list